2016-05-31 31 views
0

私はテーブルを更新したいと思います。実際には、SQLテーブルから値を取得するgridviewがあります。ページ読み込み時にgridviewの値がロードされます。既存の値を更新し、また新しい値を表名」である私はこれを行うことができますか?ちょうど、私にASP.NETをC#で動作するSQLクエリを伝える おかげ既存のレコードを更新してSQLテーブルに新しいレコードを挿入する方法は? 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 SINGLE query.Howを使用して、同じテーブルに挿入します私が新しい行 'ph'を挿入して 'update tabe'をクリックすると、 'ph'という行がすべて更新されます。

​​

+0

あなたが求めているものの多くは、Web上で利用可能なリソースがあります。ウェブ上で検索してください。 –

+0

私はweb上で検索します。私はUPDATEクエリを見つけましたが、私は同じ時間に同じテーブルにその更新と新しいレコードを挿入します。 – Ashley

答えて

1
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; 

を:Insert Data in Database Using GridView Control

編集:

このアプローチを使用して、データw i個の単一クエリ:

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"; 
+0

私は同じ時間に既存のレコードを更新し、 1つのクエリを使用しています。私は挿入クエリを求めていませんか?私の質問を理解してください。 – Ashley

+0

@Ashley:あなたはckeckupdated答え – Jaimesh

+0

できます親切に私の更新された質問を確認します。私はあなたのクエリを試してみますが、エラーが来る。ここでエラー " – Ashley

関連する問題