我找到了这个代码(Credits to:http://www.c-sharpcorner.com/UploadFile/8c19e8/dynamically-adding-and-deleting-rows-in-gridview-and-saving/),其中动态创建了Gridview。
我想添加" P_deaseases"数据表,会话[" pa_id"]以及gridview数据(使用参数)。代码只能很好地插入gridview数据,而没有添加额外的列" P_Id"。我已经在我改变代码的地方发表了一些评论。
尽管有这些变化,但仍然存在错误。
你能否建议我应该改变什么?
代码:
private string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
}
private void InsertRecords(StringCollection sc)
{
StringBuilder sb = new StringBuilder(string.Empty);
string[] splitItems = null;
const string sqlStatement = "INSERT INTO P_deasease (P_Id,Nosos,Situ,Year_d,Therapy) VALUES (@p_id)"; //I added the P_Id and the parameter
int id = Convert.ToInt32(Session["pa_id"]); //I added that
foreach (string item in sc)
{
if (item.Contains(","))
{
splitItems = item.Split(",".ToCharArray());
sb.AppendFormat("{0}('{1}','{2}','{3}','{4}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3]);
}
}
using (SqlConnection connection = new SqlConnection(GetConnectionString()))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
{
cmd.Parameters.AddWithValue("@p_id", id); //I added that
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
}
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Text = "Οι εγγραφές αποθηκεύτηκαν!";
}
protected void BtnSave_Click(object sender, EventArgs e)
{
int rowIndex = 0;
StringCollection sc = new StringCollection();
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList1");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox3");
//add them to the collections with a comma "," as the delimited values
sc.Add(string.Format("{0},{1},{2},{3}", box1.Text, box2.Text, ddl1.SelectedItem.Text, box3.Text));
rowIndex++;
}
//Call the method for executing inserts
InsertRecords(sc);
}
}
}
答案 0 :(得分:0)
您以错误的方式添加了参数。请注意,foreach添加了这样的字符串:
<already built sql expression> (s1,s2,s3,s4)
示例:
INSERT INTO P_deasease (Nosos,Situ,Year_d,Therapy) VALUES (s1,s2,s3,s4)
哪个是有效的插入子句。现在添加您的内容如下:
INSERT INTO P_deasease (P_Id,Nosos,Situ,Year_d,Therapy) VALUES (@p_id)(s1,s2,s3,s4)
根本无效。
现在您可能希望P_Id为所有新条目填充相同的值。这应该是这样的:
const string sqlStatement = "INSERT INTO P_deasease (P_Id,Nosos,Situ,Year_d,Therapy) VALUES "; //I added the P_Id and the parameter
int id = Convert.ToInt32(Session["pa_id"]); //I added that
foreach (string item in sc)
{
if (item.Contains(","))
{
splitItems = item.Split(",".ToCharArray());
sb.AppendFormat("{0}(@p_id, '{1}','{2}','{3}','{4}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2], splitItems[3]);
}
}
但说实话,这是一件奇怪的事情。如果P_Id是主键列,则会失败,因为您不能有两个具有相同主键的行。您是否真的考虑过为数据库中的列设置自动生成ID?