SqlException Connection.Open() in Device Application
本文关键字:in Device Application Connection Open SqlException | 更新日期: 2023-09-27 18:31:44
我有这个代码用于在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: SqlException at System.Data.SqlClient.SqlConnection.OnError() at System.Data.SqlClient.SqlInternalConnection.OnError() at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect() at System.Data.SqlClient.SqlInternalConnection.OpenAndLogin() at System.Data.SqlClient.SqlInternalConnection..ctor() at System.Data.SqlClient.SqlConnection.Open() at DeviceApplication1.Form1.textBox1_KeyPress() at System.Windows.Forms.Control.OnKeyPress() at System.Windows.Forms.Control.WnProc()
感谢大家的回复。它在服务器的防火墙中