繁体   English   中英

如何更新现有记录并在SQL表中插入新记录? 的SQL

[英]How to Update existing record and insert new record in SQL table ? SQL

我有一个表。我想更新表。实际上我有一个从SQL表检索值的gridview。当页面加载时,gridview加载值。我希望当我在gridview中插入新值然后在SQL Table中更新现有值并使用SINGLE query在同一个表中插入新值。我该怎么做?只告诉我在C#,ASP.NET中有效的SQL查询。

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();
}
}

gridview-image表名是'employee'首先,当我插入新行'ph'并单击'update tabe'之后,我在gridview'Name','id','address'中有3行,然后我用'ph更新所有行'

数据库映像

    foreach (GridViewRow g1 in GridView1.Rows)
                {
                    SqlConnection con = new SqlConnection(connStr);
                    com = new SqlCommand("insert into student(sid,sname,smarks,saddress) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "')", con);
                    con.Open();
                    com.ExecuteNonQuery();
                    con.Close();

                }

如果您在gridview的最后一行插入新记录,则使用以下命令获取上一个哇的索引:

Int32 index = dataGridveiw1.Rows.Count - 1;

有关更多信息,请参考本文: 使用GridView控件在数据库中插入数据

编辑:

您可以使用此方法通过单个查询插入和更新数据:

  INSERT INTO student(sid, sname, smarks) VALUES('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "') ON DUPLICATE KEY UPDATE sname="g1.Cells[1].Text", smarks="g1.Cells[2].Text";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM