我正在尝试根据查询填充我的datagridview。基本上,该查询应用于我在我的sql server中创建的视图,该视图是3表的连接。这是我的代码:
Database a = new Database();
string ConStr = @"Data Source=HO-IT-WS-06;Initial Catalog=VMS;Integrated Security=True";
con = new SqlConnection(ConStr);
con.Open();
String theDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
String query = "SELECT cnic, name, comp_name, date_in, date_out, time_in," +
"time_out, chk_in, chk_out from visitor_status where date_in= @theDate";
SqlCommand sqlCmdStatement = new SqlCommand(query, con);
sqlCmdStatement.Parameters.AddWithValue("@theDate", theDate);
//dataGridView1.DataSource = ds.Tables[0];
SqlDataAdapter dataAdapter = new SqlDataAdapter(query,con); //c.con is the connection string
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = ds.Tables[0];
例外:
************** Exception Text **************
System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@theDate".
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
at Visitor_Management_System.People_Checked_In_On_Specific_Date.button1_Click(Object sender, EventArgs e) in d:\Jaali Copy\Visitor Management System\Visitor Management System\People Checked In On Specific Date.cs:line 61
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
ClientConnectionId:1afac376-fc05-4a77-ab81-8278cb908602
然而,它在行 dataAdapter.Fill(ds); 上给出了异常,说必须声明标量值@date。这里有什么问题?为什么我的datagridview没有被填充?可以将查询应用于c#中的视图吗?
答案 0 :(得分:0)
您正尝试在@theDate参数中插入字符串值。
试试这个,
DateTime theDate = dateTimePicker1.Value;
String query = "SELECT cnic, name, comp_name, date_in, date_out, time_in, time_out," +
"chk_in, chk_out from visitor_status where date_in= @theDate";
SqlCommand sqlCmdStatement = new SqlCommand(query, con);
sqlCmdStatement.Parameters.AddWithValue("@theDate", theDate);
将SqlDataAdapter声明更改为
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlCmdStatement);
如果in_date字段或dateTimePicker1包含时间,那么你必须像这样删除它。
//Remove time from DateTimePicker
DateTime theDate = dateTimePicker1.Value.Date;
//Remove time from field
Where Convert(smalldatetime,Convert(char(10),date_in,103),103) = @theDate