简体   繁体   English

没有Windows身份验证的MS SQL ODBC连接

[英]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. 我正在尝试使用ODBC连接到MS SQL,但始终收到“用户'User-PC \\ User'登录失败”错误。

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? 知道如何使ODBC工作吗?

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. SQL连接和ODBC连接不使用相同的连接字符串。 For the ODBC connection you need to specify the driver. 对于ODBC连接,您需要指定驱动程序。

For SQL Server 2012: 对于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 然后在这里: OBDC示例 ,然后在这里: 连接字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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