如何把两个SqlCommand的参数拼凑成一个commandText? 输出你的insert命令看看,另外你的station_time列是DtaeTime类型!而你的参数是SqlDbType.VarChar,这样不对! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你的sql语句中使用了三个参数@start_stop_id,@start_hour,@start_min,而你只增加了两个参数@start_hour,@start_min 这只是我的代码的一部分,我把不关键的部分都删除掉了!虽然我的参数都是SqlDbType.VarChar类型,但我是通过拼凑才得到真正要保存进数据库的值的。比如:'2004-8-18' + ' ' + @start_hour + ':' + @start_min + ':' 问题已经解决了!我定义了一个变量来存储这个拼凑的值,在作为一个参数存入数据库。cmdInsertRunRecord = new SqlCommand(); cmdInsertRunRecord.Connection = cn; strSql = "declare @new_run_record_id int,@start_stop_id int,@start_time datetime,@end_stop_id int,@end_time datetime;"; strSql += "select @start_stop_id = stop_id from bus_stop where stop_name = @start_stop;"; strSql += "select @start_time = '" + txtDate.Text + "' + ' ' + @start_hour + ':' + @start_min + ':00';"; strSql += "select @end_stop_id = stop_id from bus_stop where stop_name = @end_stop;"; strSql += "select @end_time = '" + txtDate.Text + "' + ' ' + @end_hour + ':' + @end_min + ':00';"; strSql += "insert into run_record(run_id,work_no,oclock) values(" + intRunID.ToString() + ",@work_no,@oclock);"; strSql += "select @new_run_record_id = @@Identity;"; strSql += "insert into station_time(stop_id,stop_order,station_time,run_record_id) "; strSql += "values(@start_stop_id,1,@start_time,@new_run_record_id);"; strSql += "insert into station_time(stop_id,stop_order,station_time,run_record_id) "; strSql += "values(@end_stop_id,2,@end_time,@new_run_record_id);"; cmdInsertRunRecord.CommandText = strSql; cmdInsertRunRecord.Parameters.Add("@work_no", SqlDbType.VarChar, 50, "work_no"); cmdInsertRunRecord.Parameters.Add("@oclock", SqlDbType.VarChar, 50, "oclock"); cmdInsertRunRecord.Parameters.Add("@start_stop", SqlDbType.VarChar, 50, "start_stop"); cmdInsertRunRecord.Parameters.Add("@start_hour", SqlDbType.VarChar, 2, "start_hour"); cmdInsertRunRecord.Parameters.Add("@start_min", SqlDbType.VarChar, 2, "start_min"); cmdInsertRunRecord.Parameters.Add("@end_stop", SqlDbType.VarChar, 50, "end_stop"); cmdInsertRunRecord.Parameters.Add("@end_hour", SqlDbType.VarChar, 2, "end_hour"); cmdInsertRunRecord.Parameters.Add("@end_min", SqlDbType.VarChar, 2, "end_min"); strSql += "insert into station_time(stop_id,stop_order,station_time) ";strSql += "values(@start_stop_id,1,'" + @mytime +"');";string mytime=txtDate.Text + " "+ start_hour + ":" + start_min + ":00";....................这样更清晰些吧然后把mytime传入 @mytime里面去喽。 跟happyno7(夕丁)的做法很相似,但我是在Sql语句里定义参数,而你是在程序里面。 C#打包的程序怎么在64的系统位上运行 一个动态建立控件的问题,很急!!! web中使用TreeView控件问题 Socket,HttpWebRequest,WebClient哪种方法下载网页的速度快 页面间的调用! C#中如何获取鼠标坐标 高分求C#写的OpenGL方面的例程,或网址也行 谁能告诉我!! winform里的Datagrid怎么搞的啊~~~~~~~~~~~ C#中如何实现延时若干秒? 结构体的构造函数问题!!!急! 请教用ASP。NET和C#。NET开发网站,会用到哪些特殊的技术?,什么时候会用到WEBSERVICE?在线等,立结
虽然我的参数都是SqlDbType.VarChar类型,
但我是通过拼凑才得到真正要保存进数据库的值的。比如:'2004-8-18' + ' ' + @start_hour + ':' + @start_min + ':'
在作为一个参数存入数据库。cmdInsertRunRecord = new SqlCommand();
cmdInsertRunRecord.Connection = cn;
strSql = "declare @new_run_record_id int,@start_stop_id int,@start_time datetime,@end_stop_id int,@end_time datetime;";
strSql += "select @start_stop_id = stop_id from bus_stop where stop_name = @start_stop;";
strSql += "select @start_time = '" + txtDate.Text + "' + ' ' + @start_hour + ':' + @start_min + ':00';";
strSql += "select @end_stop_id = stop_id from bus_stop where stop_name = @end_stop;";
strSql += "select @end_time = '" + txtDate.Text + "' + ' ' + @end_hour + ':' + @end_min + ':00';";
strSql += "insert into run_record(run_id,work_no,oclock) values(" + intRunID.ToString() + ",@work_no,@oclock);";
strSql += "select @new_run_record_id = @@Identity;";
strSql += "insert into station_time(stop_id,stop_order,station_time,run_record_id) ";
strSql += "values(@start_stop_id,1,@start_time,@new_run_record_id);";
strSql += "insert into station_time(stop_id,stop_order,station_time,run_record_id) ";
strSql += "values(@end_stop_id,2,@end_time,@new_run_record_id);";
cmdInsertRunRecord.CommandText = strSql;
cmdInsertRunRecord.Parameters.Add("@work_no", SqlDbType.VarChar, 50, "work_no");
cmdInsertRunRecord.Parameters.Add("@oclock", SqlDbType.VarChar, 50, "oclock");
cmdInsertRunRecord.Parameters.Add("@start_stop", SqlDbType.VarChar, 50, "start_stop");
cmdInsertRunRecord.Parameters.Add("@start_hour", SqlDbType.VarChar, 2, "start_hour");
cmdInsertRunRecord.Parameters.Add("@start_min", SqlDbType.VarChar, 2, "start_min");
cmdInsertRunRecord.Parameters.Add("@end_stop", SqlDbType.VarChar, 50, "end_stop");
cmdInsertRunRecord.Parameters.Add("@end_hour", SqlDbType.VarChar, 2, "end_hour");
cmdInsertRunRecord.Parameters.Add("@end_min", SqlDbType.VarChar, 2, "end_min");
strSql += "values(@start_stop_id,1,'" + @mytime +"');";string mytime=txtDate.Text + " "+ start_hour + ":" + start_min + ":00";
....................
这样更清晰些吧
然后把mytime传入 @mytime里面去喽。
而你是在程序里面。