简体   繁体   English

Windows CE设备中的SQL异常

[英]SQL Exception in Windows CE device

错误信息 I'm working on Windows CE application, I was trying to connect to server database from the device and fetch some information from db on button click, below is the code I tried, 我正在使用Windows CE应用程序,试图从设备连接到服务器数据库,并在单击按钮时从db获取一些信息,以下是我尝试的代码,

SqlConnection conn = new SqlConnection("Data Source=192.168.0.0;Initial Catalog=DashReport;Integrated Security=SSPI; User ID=SA;Password=Admin@123;");

SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT STORE, YEAR,DATE FROM TOPSALES WHERE MONTH = " + txtcode.Text + ";";
// cmd.CommandType = CommandType.Text;
cmd.Connection = conn;

conn.Open();

using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
    DataTable dt = new DataTable();
    sda.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        gvDataGrid.DataSource = dt;
    }
    else
    {
        MessageBox.Show("No data found");
    }
}

conn.Close();

but while running the application I'm getting a SqlException . 但是在运行应用程序时,我得到了SqlException It seems there is something wrong with the connection string. 连接字符串似乎有问题。 What is the right method to do it? 什么是正确的方法?

You cannot have both the integrated security and specify a specific user id and password at the same time. 您不能同时拥有集成安全性和同时指定特定的用户ID和密码。 Since you have the Integrated Security=SSPI; 由于您具有 Integrated Security=SSPI; , that will take precedence and your connection tries to connect with the currently logged in Windows user. ,它将具有优先权,并且您的连接尝试与当前登录的Windows用户连接。

Most likely , from a Windows CE device, you want to use the specific User I 在Windows CE设备上, 最有可能要使用特定的用户I

string connStr = "Data Source=192.168.0.0;Initial Catalog=DashReport;User ID=SA;Password=Admin@123;"
SqlConnection conn = new SqlConnection(connStr);

And another word of caution from long time SQL Server users, admins, and programmers: you should NEVER EVER use the built-in sa account! 长期使用SQL Server的用户,管理员和程序员也要提防一个警告: 永远不要使用内置的sa帐户! Just don't do it - use another account (possibly one you create specifically for this application). 只是不要这样做-使用另一个帐户(可能是您专门为此应用程序创建的帐户)。

Have you tried using the Server Explorer of Visual Studio, then connect to the database, get the Connection String via Properties Window, and use it as your connection string? 您是否尝试过使用Visual Studio的服务器资源管理器,然后连接到数据库,通过“属性”窗口获取“连接字符串”,并将其用作您的连接字符串?

Just some kind of assurance that your connection to the database is the same as your code. 只是某种保证,您与数据库的连接与您的代码相同。

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

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