C# for MS SQL connection

VAer-4038 771 Reputation points
2021-11-30T23:21:46.403+00:00

I am not an IT professional, and I would like to ask a question related to server connection.

Usually, I use ODBC connection string (such as below sample code), the only reason is: I can view(find out) server database property via Control Panel > Administrative Tools > ODBC Data Sources. Current code works fine for me.

Question: In general, which way of connection is better for MS SQL? Why? What are the difference?

If not using ODBC connection, what are substitutions for OdbcConnection/OdbcCommand/OdbcDataReader etc ?

Thanks.

OdbcConnection/OdbcCommand/OdbcDataReader 

OdbcConnection Cn = new OdbcConnection(GlobalVariables.ConnectionString);
            Cn.Open();
            string QueryM = "SELECT * From TableName";
            OdbcCommand cmdM = new OdbcCommand(QueryM, Cn);
            OdbcDataReader drM = cmdM.ExecuteReader();


OdbcDataAdapter AdapterTD = null;
        DataSet dSetTD = null;
Developer technologies C#
{count} votes

Accepted answer
  1. Ken Tucker 5,861 Reputation points
    2021-12-01T01:03:08.8+00:00

    You should use the classes in the SqlClient namespace. They are for connecting to Microsoft SqlServer

             SqlConnection Cn = new SqlConnection(GlobalVariables.ConnectionString);
             Cn.Open();
             string QueryM = "SELECT * From TableName";
             SqlCommand cmdM = new SqlCommand(QueryM, Cn);
             SqlDataReader drM = cmdM.ExecuteReader();
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.