用c#在Windows窗体应用程序中显示存储过程中的数据
本文关键字:存储过程 存储 过程中 数据 显示 Windows 窗体 应用程序 | 更新日期: 2023-09-27 18:09:08
我使用以下代码,但它在SqlDataReader
上抛出错误:
SqlConnection cn = new SqlConnection(@"Data Source=.;Initial Catalog=Employee1;Persist Security Info=True;User ID=sa;Password='786'");
SqlCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.Date";
cmd.Parameters.AddWithValue("@DOB", Convert.ToDateTime(textBox1.Text));
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Employee ID", typeof(string)), new DataColumn("Employee Name", typeof(string)), new DataColumn("Status",typeof(char)) });
dt.Rows.Add();
try
{
cn.Open();
SqlDataReader res;
res = cmd.ExecuteReader();
MessageBox.Show("COmmand Executed Successfully");
int i = 0;
if (res.Read())
{
dt.Rows.Add(res[i].ToString(), res[++i].ToString(), res[++i].ToString());
}
res.Close();
cn.Close();
dataGridView1.DataSource = dt;
}
catch (Exception exp)
{
MessageBox.Show(exp.StackTrace);
}
的例外是:
在System.Data.Sqlaient.Sq1Connection。在System.Data.Sq1Client.SqlInternalConnection上OnError(SqlException异常,Boolean breakConnection, Action'1 wrapCloselnAction)在System.Data.Sq1Client.TdsParser上OnError(SqlException异常,Boolean breakConnection, Action'1 wrapCloselnAction)在System.Data.SqIClient.TdsParser. tdsparserstateobject中抛出异常并警告(tdsparserstateobject)。TryRun(RunBehavior RunBehavior, SqICommand cmdHandler, SqIDataReader dataStream, BulkCopySimpleResultSet, bulkCopyHandler, TdsParserStateObject, statobj, Boolean&在System.Data.Sq1Client.Sq1DataReader.get_MetaData() at System.Data.Sq1Client.Sq1Command. dataReady ()FinishExecuteReader(Sq1DataReader ds, RunBehavior RunBehavior, String resetOptionsString)在System.Data.Sq1Client.Sq1Command。RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior RunBehavior,布尔返回流,布尔异步,Int32超时,任务&在System.Data.Sq1Client.Sq1Command. task, Boolean asyncWrite, SqIDataReader, Boolean escri beParameterEncryptionRequest。RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior RunBehavior,布尔返回流,字符串方法,TaskCompletionSourcel完成,Int32超时,任务&在System.Data.Sq1Client.Sq1Command中执行asyncWrite操作。RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior RunBehavior,布尔返回流,字符串方法)在System.Data.Sq1Client.Sq1Command。ExecuteReader(CommandBehavior行为,字符串方法)在System.Data.Sq1Client.Sq1Command。出席时的执行者。c:'Users'admin'Desktop'Attendance'Attendance'Form4.cs:第72行
首先去掉密码中的单引号。其次,当添加行时,当使用res[i]时,i是列的索引,因此不需要增加此值。您还可以使用using块来关闭连接并在此循环中处置对象。您也不需要dt.Rows.Add(),因为它用于添加行而不是列。下面是你的方法的重构:
try
{
using (SqlConnection cn = new SqlConnection(@"Data Source=.; Initial Catalog=Employee1;Persist Security Info=True;User ID=sa;Password=786"))
{
using (SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "dbo.Date";
cmd.Parameters.AddWithValue("@DOB", Convert.ToDateTime(textBox1.Text));
using (DataTable dt = new DataTable())
{
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Employee ID", typeof(string)), new DataColumn("Employee Name", typeof(string)), new DataColumn("Status", typeof(char)) });
cn.Open();
using (SqlDataReader res = cmd.ExecuteReader())
{
MessageBox.Show("COmmand Executed Successfully");
while(res.Read())
{
dt.Rows.Add(res[0].ToString(), res[1].ToString(), res[2].ToString());
}
dataGridView1.DataSource = dt;
}
}
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.StackTrace);
}