执行命令时,会给出以下SqlException
。已建立连接。客户端操作系统是一个模拟Windows CE,用于查询SQL Server 2012数据库。
此行引发异常:
SqlDataReader reader = Shared.command.GetCommand().ExecuteReader();
Shared是一类带有初始化静态对象的变量。
class Connect
{
SqlConnection connect = new SqlConnection();
public SqlConnection GetConnection()
{
return connect;
}
public void SetConnection(string server, string database, string user, string password)
{
connect.ConnectionString = "Data Source=" + server + "; Initial Catalog=" + database + "; User ID=" + user + "; Password=" + password + ";";
}
public void Open()
{
try
{
connect.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
class Command
{
public Command()
{
command.Connection = Shared.connect.GetConnection();
}
SqlCommand command = new SqlCommand();
public SqlCommand GetCommand()
{
return command;
}
public void SetCommand(string commandText, List<SqlParameter> parameters)
{
command.CommandText = commandText;
foreach (SqlParameter parameter in parameters)
command.Parameters.Add(parameter);
}
}
List <SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("BARCODE", tbxBarcode.Text));
Shared.command.SetCommand("SELECT * FROM table WHERE BARCODE_STRING = @BARCODE", parameters);
IV_Barcode barcode = new IV_Barcode();
DataTable table = barcode.Execute();
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at Inventory_Checker.IV_Barcode.Execute()
at Inventory_Checker.frmMain.tbxBarcode_LostFocus(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnLostFocus(EventArgs e)
at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at Inventory_Checker.Program.Main()
答案 0 :(得分:0)
问题已经解决。我的SQL参数是导致问题的原因。
以下内容必须更改......
List <SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("BARCODE", tbxBarcode.Text));
Shared.command.SetCommand("SELECT * FROM table WHERE BARCODE_STRING = @BARCODE", parameters);
IV_Barcode barcode = new IV_Barcode();
DataTable table = barcode.Execute();
......对此:
List <SqlParameter> parameters = new List<SqlParameter>();
parameters.Add(new SqlParameter("@BARCODE", tbxBarcode.Text));
Shared.command.SetCommand("SELECT * FROM table WHERE BARCODE_STRING = @BARCODE", parameters);
IV_Barcode barcode = new IV_Barcode();
DataTable table = barcode.Execute();