简体   繁体   中英

MS SQL ODBC Connection with NO Windows Authentication

I'm trying to connect to MS SQL using ODBC but keep getting the "Login failed for user 'User-PC\\User'" error.

web.config 
<add name="SQLDbConnection" connectionString="Server=127.0.0.1; Database=HMS; Integrated Security=false; User Id=sa; password=root" providerName="System.Data.Odbc "/>

C#
        string query = "...";
        OdbcConnection msSQLConnection = new OdbcConnection(strConnection);
        OdbcCommand command = new OdbcCommand(query, msSQLConnection);
        command.Connection.Open();

I tried using the below and it's ok. Any idea how I can get ODBC to work?

using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString()))
        {
            SqlCommand cmd = new SqlCommand("SELECT COMPANY_ID from COMPANY", cn);
            cn.Open();
            SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            rdr.Read();
        }

SQL Connection and ODBC Connection do not use the same connection strings. For the ODBC connection you need to specify the driver.

For SQL Server 2012:

Driver={SQL Server Native Client 11.0};Server=127.0.0.1;Database=HMS;Uid=sa;Pwd=root;

You should use something like this :

    DRIVER={MySQL ODBC 3.51 Driver}; SERVER=127.0.0.1; DATABASE=HMS; USER=sa; PASSWORD=root;

Then take a look in here : OBDC example and then in here : Connection strings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM