设备应用程序中的SqlException Connection.Open()

时间:2014-11-22 18:02:29

标签: c# sql-server windows-ce

我的代码用于在Windows CE上运行的价格检查设备

但是我在行上找到了SqlException

cmd.Connection.Open();

连接字符串或代码的其他部分出现问题吗?

constring = "Password=12345;Persist Security Info=True;User ID=sa;Initial Catalog=Retail;Data Source=111.111.111.111";

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter)
    {
        for (int i = 0; i < textBox1.Text.Length; i++)
        {
            if (char.IsDigit(textBox1.Text, 0) == false)
            {
                MessageBox.Show("Invalid Barcode");
                textBox1.Focus();
            }
            else
            {
                try
                {
                    Sql = "Select i.a_name,u.retailprice From sys_item i,sys_item_units u where i.itemean =u.itemean and u.barcode='" + textBox1.Text + "'";
                    cmd = new SqlCommand(Sql, new SqlConnection(constring));
                    da = new SqlDataAdapter(Sql, constring);
                    cmd.Connection.Open();

                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        label1.Text = dr[0].ToString();
                        label2.Text = dr[1].ToString();
                    }

                    cmd.Connection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    textBox1.Text = "";
                    textBox1.Focus();
                }
            }
        }

        e.Handled = true;
    }
}

例外:

System.Data.SqlClient.SqlException:System.Data.SqlClient.SqlConnection.OnError()处的System.Data.SqlClient.SqlInternalConnection.OnError()处于系统的System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()处的SqlException System.Data.SqlClient.TdsParser.Connect()位于DeviceApp上的System.Data.SqlClient.SupInternalConnection..ctor()处的System.Data.SqlClient.SqlInternalConnection.OpenAndLogin()处于DeviceApplication1的System.Data.SqlClient.SqlConnection.Open()处。在System.Windows.Forms.Control.WnProc()的System.Windows.Forms.Control.OnKeyPress()中的Form1.textBox1_KeyPress()

1 个答案:

答案 0 :(得分:0)

感谢所有回复。它位于服务器的防火墙中