Sql server之路 (五)插入多条数据

public bool GetDataUsingDataTaskReason(StationReasonCollection reason)
{
try
{
strCon.Open();
DateTime dt = new DateTime();
dt = System.DateTime.Now;
string strFu = dt.ToString("yyyy-MM-dd HH:mm");
TaskReasonInfo ts=null;
ts=new TaskReasonInfo();
ts.StationId="B1237";
ts.workTaskFrom="手工录入";
ts.workTaskTime=strFu;
ts.workTaskTypeId="巡检";
ts.workTaskXiadaren="齐经理";
ts.workYuanyin="ceshi";
reason.Add(ts);

ts = new TaskReasonInfo();
ts.StationId = "B1238";
ts.workTaskFrom = "手工录入";
ts.workTaskTime = strFu;
ts.workTaskTypeId = "巡检";
ts.workTaskXiadaren = "齐经理";
ts.workYuanyin = "ceshi";
reason.Add(ts);

//都可以用string strSql = " INSERT INTO WorkTasks(stationId,workTaskTypeId,workTaskTime,workTaskFrom,workTaskXiadaren,workYuanyin) SELECT @stationId,@workTaskTypeId,@workTaskTime,@workTaskFrom,@workTaskXiadaren,@workYuanyin ";
string strSql = "INSERT INTO WorkTasks(stationId,workTaskTypeId,workTaskTime,workTaskFrom,workTaskXiadaren,workYuanyin) VALUES(@stationId,@workTaskTypeId,@workTaskTime,@workTaskFrom,@workTaskXiadaren,@workYuanyin)";
SqlCommand SqlCmd = new SqlCommand();
SqlCmd.Connection = strCon;
SqlCmd.CommandText = strSql;

foreach (var info in reason)
{
SqlParameter para = null;
SqlCmd.Parameters.Clear();
para = new SqlParameter("@stationId", info.StationId);
SqlCmd.Parameters.Add(para);
para = new SqlParameter("@workTaskTypeId", info.workTaskTypeId);
SqlCmd.Parameters.Add(para);
para = new SqlParameter("@workTaskTime", info.workTaskTime);
SqlCmd.Parameters.Add(para);
para = new SqlParameter("@workTaskFrom", info.workTaskFrom);
SqlCmd.Parameters.Add(para);
para = new SqlParameter("@workTaskXiadaren", info.workTaskXiadaren);
SqlCmd.Parameters.Add(para);
para = new SqlParameter("@workYuanyin", info.workYuanyin);
SqlCmd.Parameters.Add(para);
SqlCmd.ExecuteNonQuery();
}

return true;
}
catch (Exception)
{
return false;
throw;
}
}

上一篇:使用Notepad++将多行数据合并成一行


下一篇:Git Submodule使用完整教程