我正在尝试使用具有两个表的存储过程插入数据。第一个表是通过文本框的数据,第二个数据是通过我存储在数据库中并传递以插入的网格。问题是当读取数据表并插入它时说有太多的参数恰好添加到 for 循环中。有什么建议如何作为 SP 处理吗?提前致谢。
代码:
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = strConnection;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandTimeout = 120;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insFamilyDetails";
cmd.Parameters.AddWithValue("@strHusbandName", strHusbandName);
cmd.Parameters.AddWithValue("@strRelation", strRelation);
....
....
// Child Details
for (int i = 0; i < strChildredDetails.Rows.Count; i++)
{
cmd.Parameters.AddWithValue("@strChildName", strChildredDetails.Rows[i][0].ToString());
cmd.Parameters.AddWithValue("@strDOB", strChildredDetails.Rows[i][1]);
cmd.Parameters.AddWithValue("@strBaptisedon", strChildredDetails.Rows[i][2]);
cmd.Parameters.AddWithValue("@strFirstComOn", strChildredDetails.Rows[i][3]);
cmd.Parameters.AddWithValue("@strConfirmedOn", strChildredDetails.Rows[i][4]);
cmd.Parameters.AddWithValue("@strMarried", "0");
cmd.Parameters.AddWithValue("@strAlive", "1");
}
conn.Open();
ReturnValue = Convert.ToBoolean(cmd.ExecuteNonQuery());
conn.Close();
}
catch (Exception e)
{
DL_LogAppErrors(e.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name, "Insert Family Details");
return ReturnValue;
}
return ReturnValue;