是的,你可以同时插入两个表。请检查以下代码:
说明:您原来的插入查询是错误的,我修改了它。
public void InsertIntoDataBase(int itemCode, string itemName, string itemImage)
{
string connString = ConfigurationManager.ConnectionStrings[1].ToString();
string query1 = @"insert into tbl_item (ItemCode,ItemName) values(@itemcode,@itemname)";
string query2 = @"insert into tbl_image (ItemCode,ItemImage) values(@itemcode,@itemimage)";
SqlConnection conn = new SqlConnection(connString);
try
{
// Exc]ecute the first query.
SqlCommand cmd = new SqlCommand(query1, conn);
cmd.Parameters.Add("@itemcode", SqlDbType.Int, 10, "ItemCode").Value = itemCode; // Pass the actual Item code
cmd.Parameters.Add("@itemname", SqlDbType.Text, 20, "ItemName").Value = itemName; //Pass the actual Item name
cmd.ExecuteNonQuery();
// Exc]ecute the second query.
cmd = new SqlCommand(query2, conn);
cmd.Parameters.Add("@itemcode", SqlDbType.Int, 10, "ItemCode").Value = itemCode; // Pass the actual Item code
cmd.Parameters.Add("@itemimage", SqlDbType.Text, 20, "ItemImage").Value = itemImage; // Pass the actual Item image
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
}
finally
{
conn.Close();
}
}
那是真正的代码,因为我觉得你有列名换参数,再加上你确定你的表'Image'是不是基于'tblItem'一个看法? – Habib 2013-05-06 10:46:22
_“当我点击提交按钮时,这些记录被插入到两个表中”_ - 你确定吗?你只在按钮点击时执行一个查询吗?数据库是否包含任何触发器,或者确实是@Habib所说的视图?这太难以解决,请显示更多信息。 – CodeCaster 2013-05-06 10:48:12
谢谢Habib,是的,它是一个真正的代码首先我尝试了两个领域,然后我将添加 – Vijaya 2013-05-06 10:49:28