关于C#中ExecuteNonQuery()的问题 5
privatevoidbutton1_Click_1(objectsender,EventArgse){stringstartTime1=dateTimePicker1....
private void button1_Click_1(object sender, EventArgs e)
{
string startTime1 = dateTimePicker1.Value.Date.ToShortDateString();
string startTime2 = dateTimePicker2.Value.ToString("HH:mm:ss");
string endTime1 = dateTimePicker3.Value.Date.ToShortDateString();
string endTime2 = dateTimePicker4.Value.ToString("HH:mm:ss");
MessageBox.Show(startTime1 +" "+ startTime2);
MessageBox.Show(endTime1 + " " + endTime2);
//数据库连接字符串
string consql = "Data Source=.;Initial Catalog = janitor_blank;uid=sa;pwd=12345678";
string sql = "process";
//建立连接
SqlConnection con = new SqlConnection(consql);
//建立查询命令,表示对数据库执行一个SQL语句或存储过程
try
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@startTime", SqlDbType.DateTime).Value = startTime1 + " " + startTime2;
cmd.Parameters.Add("@endTime", SqlDbType.DateTime).Value = endTime1 + " " + endTime2;
cmd.CommandTimeout = 0;
con.Open();
//MessageBox.Show(cmd.ExecuteNonQuery().ToString());
int count = cmd.ExecuteNonQuery();//这个是返回受影响的行数,当插入重复值时数据库会报防止插入重复键,语句终止错误
MessageBox.Show("执行完成!已成功插入" + count + "条数据");
问题:执行的是一个存储过程,cmd.ExecuteNonQuery(),但插入数据有重复值时无法执行到MessageBox.Show这一步 展开
{
string startTime1 = dateTimePicker1.Value.Date.ToShortDateString();
string startTime2 = dateTimePicker2.Value.ToString("HH:mm:ss");
string endTime1 = dateTimePicker3.Value.Date.ToShortDateString();
string endTime2 = dateTimePicker4.Value.ToString("HH:mm:ss");
MessageBox.Show(startTime1 +" "+ startTime2);
MessageBox.Show(endTime1 + " " + endTime2);
//数据库连接字符串
string consql = "Data Source=.;Initial Catalog = janitor_blank;uid=sa;pwd=12345678";
string sql = "process";
//建立连接
SqlConnection con = new SqlConnection(consql);
//建立查询命令,表示对数据库执行一个SQL语句或存储过程
try
{
SqlCommand cmd = new SqlCommand(sql, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@startTime", SqlDbType.DateTime).Value = startTime1 + " " + startTime2;
cmd.Parameters.Add("@endTime", SqlDbType.DateTime).Value = endTime1 + " " + endTime2;
cmd.CommandTimeout = 0;
con.Open();
//MessageBox.Show(cmd.ExecuteNonQuery().ToString());
int count = cmd.ExecuteNonQuery();//这个是返回受影响的行数,当插入重复值时数据库会报防止插入重复键,语句终止错误
MessageBox.Show("执行完成!已成功插入" + count + "条数据");
问题:执行的是一个存储过程,cmd.ExecuteNonQuery(),但插入数据有重复值时无法执行到MessageBox.Show这一步 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询