Reputation: 95
I'm an ASP.NET web developer. I have created a Windows application for retrieving data from SQL Server. Here's my app config and class file for retrieving data from SQL Server:
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<appSettings>
</appSettings>
<connectionStrings>
<add name="ConnString" connectionString="Data Source=INFY\SQLEXPRESS;Initial Catalog=NEWBULKSMS;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Code that attempts to connect to SQL Server
using System;
using System.Collections.Generic;
using System.Collections;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
public class Mysql
{
public static string ConString = System.Configuration.ConfigurationManager.AppSettings.Get("ConnString");
public static string InsertResult = "0";
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
private static MySqlParameter[] DiscoverSpParameterSet(string spName, bool includeReturnValueParameter)
{
using (MySqlConnection cn = new MySqlConnection(Mysql.ConString))
{
using (MySqlCommand cmd = new MySqlCommand(spName, cn))
{
cn.Open();
cmd.CommandType = CommandType.StoredProcedure;
MySqlCommandBuilder.DeriveParameters(cmd);
MySqlParameter[] discoveredParameters = new MySqlParameter[cmd.Parameters.Count]; ;
cmd.Parameters.CopyTo(discoveredParameters, 0);
return discoveredParameters;
}
}
}
}
Is that the right way to connect to a SQL Server database?
Upvotes: 0
Views: 120
Reputation: 76
Change your connection string like bellow and you have to change from MySql to SQL and add using System.Data.SqlClient insted of using MySql.Data.MySqlClient that you already added in your project.................
public static string connectionString = WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
public static string InsertResult = "0";
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
private static SqlParameter[] DiscoverSpParameterSet(string spName, bool includeReturnValueParameter)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(spName, sqlConnection))
{
connection.Open();
command.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(command);
SqlParameter[] discoveredParameters = new SqlParameter[cmcommandd.Parameters.Count];
discoveredParameters=command.ExecuteNonQuery();
ccommandn.Close();
return discoveredParameters;
}
}
}
Upvotes: 0
Reputation: 82524
Your connection string seems fine but your code is attempting to connect to SQL Server using MySql classes. This is wrong. Instead of MySqlConnection
you should be using SqlConnection
, instead of MySqlCommand
- SqlCommand
and so on.
All the classes that starts with MySql
belong to the MySql.Data.MySqlClient
namespace are are designed to work with MySql.
To work with Sql Server you need to use the classes that starts with Sql
and belong to the System.Data.SqlClient
namespace.
I would start by removing the using MySql.Data.MySqlClient;
line from the top of the code and then fix all the compilation errors by replacing MySql classes with Sql Server classes.
Upvotes: 4