Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在SQL表中同时更新现有记录和插入新记录?ASP.NETC_C#_Asp.net_Sql Server_Gridview - Fatal编程技术网

C# 如何在SQL表中同时更新现有记录和插入新记录?ASP.NETC

C# 如何在SQL表中同时更新现有记录和插入新记录?ASP.NETC,c#,asp.net,sql-server,gridview,C#,Asp.net,Sql Server,Gridview,我有一个表。我想更新表。实际上我有一个gridview,它从SQL表中检索值。当页面加载时,gridview加载值。我想在gridview中插入新值,然后在SQL表中插入现有值更新,并且使用单个查询在同一个表中插入新值。我该怎么做?请告诉我SQL查询它在C、ASP.NET中工作。我要求使用一个查询同时更新现有记录并在同一个表中插入新记录。谢谢 public void insert(object sender, EventArgs e) { string user = Session["name"

我有一个表。我想更新表。实际上我有一个gridview,它从SQL表中检索值。当页面加载时,gridview加载值。我想在gridview中插入新值,然后在SQL表中插入现有值更新,并且使用单个查询在同一个表中插入新值。我该怎么做?请告诉我SQL查询它在C、ASP.NET中工作。我要求使用一个查询同时更新现有记录并在同一个表中插入新记录。谢谢

public void insert(object sender, EventArgs e)
{
string user = Session["name"].ToString();
SqlConnection cnn = new SqlConnection("Data Source=HAMEED_KHAN\\SQLEXPRESS;Initial Catalog=db_compiler;Integrated Security=True");
SqlCommand cmd3 = new SqlCommand("SELECT User_ID from tbl_user WHERE User_Name='" + user + "'", cnn);
cnn.Open();
string id = cmd3.ExecuteScalar().ToString();
int ID = Int32.Parse(id);
Session["ID"] = ID;
string d = Session["value"].ToString();
SqlCommand cmd2 = new SqlCommand("SELECT Database_id FROM Create_db WHERE Database_Name='" + d + "'", cnn);
Response.Write("<script>Var Z=Prompt('Enter Table Name');</script>");
string dbid = cmd2.ExecuteScalar().ToString();
cnn.Close();
int D_ID = Int32.Parse(dbid);
string str = "";
string type = "";
for (int i = 0; i < GridView2.Rows.Count; i++)
{
str = GridView2.Rows[i].Cells[1].Text.ToString();
type = GridView2.Rows[i].Cells[2].Text.ToString();
 string Name = GridView2.Rows[i].Cells[1].Text.ToString();
 string Type = GridView2.Rows[i].Cells[2].Text.ToString();
 string size = GridView2.Rows[i].Cells[3].Text.ToString();
 CheckBox allow = GridView2.Rows[i].Cells[4].Controls[0] as CheckBox;
 CheckBox primary = GridView2.Rows[i].Cells[5].Controls[0] as CheckBox;
 string UserID = Session["ID"].ToString();
int UID = Int32.Parse(UserID);
string date = DateTime.Now.ToString();
string A = (allow.Checked == true ? "NULL" : "NOT NULL");
string P = (primary.Checked == true ? "PRIMARY KEY" : "");
string Table = Session["TBL_NAME"].ToString();
string queryy ="USE db_compiler UPDATE tbl_field SET Column_Name='" + Name + "', Data_Type='" + Type + "',Size='" + size + "',Database_id='" + D_ID + "',Allow_Null_='" + (allow.Checked == true ? "true" : "false") + "',Primary_Key_='" + (primary.Checked == true ? "true" : "false") + "',User_id='" + UID + "',Date='" + date + "' WHERE Table_Name='" + Table + "' IF @@ROWCOUNT=0 insert into tbl_field (Table_Name,Column_Name,Data_Type,Size,Database_id,Allow_Null_,Primary_Key_,User_id,Date) VALUES('" + Table + "','" + Name + "','" + Type + "','" + size + "','" + D_ID + "','" + (allow.Checked == true ? "true" : "false") + "','" + (primary.Checked == true ? "true" : "false") + "','" + UID + "','" + date + "')";
 SqlCommand cmd = new SqlCommand(queryy, cnn);
 SqlDataAdapter ad = new SqlDataAdapter(cmd);
 cnn.Open();
 cmd.ExecuteNonQuery();
 cnn.Close();
 }
 }

这是我的网格视图图像表名称是employee first我在网格视图名称中有3行,id,'address',当我插入新行ph并单击update Table时,然后我用ph更新所有行。

听起来你真的想在数据库中创建更新触发器。除此之外,你还需要两个陈述,可能在一个事务中。我不能使用两个事务,因为如果我先使用insert语句,那么数据将被覆盖;如果先使用update语句,那么当用户输入新列并按下updateTable按钮时,则在现有表中找不到新列,然后我无法更新我不想要触发器,当用户单击时我想要触发器updateTable按钮,然后gridview中的所有值替换旧值,并在同一个表中插入新值。简单。听起来您真的想在数据库中创建更新触发器。除此之外,你还需要两个陈述,可能在一个事务中。我不能使用两个事务,因为如果我先使用insert语句,那么数据将被覆盖;如果先使用update语句,那么当用户输入新列并按下updateTable按钮时,则在现有表中找不到新列,然后我无法更新我不想要触发器,当用户单击时我想要触发器updateTable按钮,然后gridview中的所有值替换旧值,并在同一个表中插入新值。Simple。