2012-08-08 79 views
2

我有一个datagridview,并在表中的一些数据。 在DataGridView允许用户输入数据的新行如何从datagridview向数据库中插入新添加的数据行?

的问题在这里:

我想知道我如何能得到数据的新插入的行(无论多少行已添加)到数据库中,而不添加将被复制的现有数据。

有人吗?

编辑:即时通讯使用SQL数据库,这是我的datagridview。

enter image description here

显示的数据是已经在数据库里面,现在,如果用户插入什么数据的新的多行插入的datagridview?我应该放什么代码?

我的代码如下:

private void button1_Click(object sender, EventArgs e) 
     { 
      con = new System.Data.SqlClient.SqlConnection(); 
      con.ConnectionString = "Data Source=tcp:SHEN-PC,49172\\SQLEXPRESS;Initial Catalog=LSEStock;Integrated Security=True"; 
      con.Open(); 
      SqlDataAdapter da = new SqlDataAdapter(); 

      for (int i = 0; i<dataGridView1.Rows.Count; i++) 
      { 

       String insertData = "INSERT INTO CostList(SupplierName, CostPrice, PartsID) VALUES (@SupplierName, @CostPrice, @PartsID)" ; 
       SqlCommand cmd = new SqlCommand(insertData, con); 
       cmd.Parameters.AddWithValue("@SupplierName", dataGridView1.Rows[i].Cells[0].Value); 
       cmd.Parameters.AddWithValue("@CostPrice", dataGridView1.Rows[i].Cells[1].Value); 
       cmd.Parameters.AddWithValue("@PartsID", textBox1.Text); 
       da.InsertCommand = cmd; 
       cmd.ExecuteNonQuery(); 
      } 

      con.Close(); 
+2

你能提供有关要添加什么类型的数据多一点细节,什么是依托数据库? – Moop 2012-08-08 02:14:49

回答

0

你有什么样的数据库?这里是我的oracle数据库的源代码。如果它的SQL数据库替换:with @。

的源代码:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ProjectCISConnectionString %>" ProviderName="<%$ConnectionStrings:ProjectCISConnectionString.ProviderName %>" 
      SelectCommand="SELECT * FROM CIS.CIS_TRANS ORDER BY ID ASC" 
      DeleteCommand="DELETE FROM CIS.CIS_TRANS WHERE ID = :ID" 
      InsertCommand="INSERT INTO CIS.CIS_TRANS (TRANS_CD,FUND_CD,BSA_CD,DP_TYPE,TRANS_CD_DESC) VALUES (:TRANS_CD,:FUND_CD,:BSA_CD,:DP_TYPE,:TRANS_CD_DESC)" 
      UpdateCommand="UPDATE CIS.CIS_TRANS SET TRANS_CD = :TRANS_CD, FUND_CD = :FUND_CD, BSA_CD = :BSA_CD, DP_TYPE = :DP_TYPE, TRANS_CD_DESC =:TRANS_CD_DESC WHERE ID = :ID"> 

     </asp:SqlDataSource> 

你也将需要一个查询页面和一些其他的东西,请让我知道更多的信息。

1

我的继承人插入,取消并删除声明:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      if (e.CommandName == "Insert") //- this is needed to explain that the INSERT command will only work when INSERT is clicked 
      { 
       gv.DataBind(); 

       DataTable d = dbcon.GetDataTable("SELECT * FROM CIS.CIS_TRANS ORDER BY ID DESC", "ProjectCISConnectionString"); 

       string transCode = "", fundCode = "", BSA_CD = "", DP_TYPE = ""; 

       if (d.Rows.Count > 0) 
       { 
        transCode = d.Rows[0]["TRANS_CD"].ToString(); 
        fundCode = d.Rows[0]["FUND_CD"].ToString(); 
        BSA_CD = d.Rows[0]["BSA_CD"].ToString(); 
        DP_TYPE = d.Rows[0]["DP_TYPE"].ToString(); 

        if (transCode.Trim().Length > 0) 
        { 
         dbcon.Execute("INSERT INTO CIS.CIS_TRANS (ID,TRANS_CD) VALUES(CIS.S_CIS_TRANS.nextval,'')", "ProjectCISConnectionString"); 

         gv.DataBind(); 
        } 
       } 
gv.EditIndex = gv.Rows.Count - 1; 

     } 
     else if (e.CommandName == "Cancel") 
     { 
      DataTable d = dbcon.GetDataTable("SELECT * FROM CIS.CIS_TRANS ORDER BY ID DESC", "ProjectCISConnectionString"); 

      string transCode = ""; 

      if (d.Rows.Count > 0) 
      { 
       transCode = d.Rows[0]["TRANS_CD"].ToString(); 

       if (transCode.Trim().Length == 0) 
       { 
        dbcon.Execute(string.Format("DELETE CIS.CIS_TRANS WHERE ID = '{0}'", d.Rows[0]["ID"]), "ProjectCISConnectionString"); 

        gv.DataBind(); 
       } 
      } 
0

我做这样的事情。快速方便,似乎很好地工作:

cmd1.Connection = this.sqlConnection1; 
this.sqlConnection1.Open(); 

foreach (GridViewRow row in your_grid.Rows) 
     { 
      Label id = (Label)row.FindControl("your_control"); //what ever control you are using 

      SqlCommand cmd1 = new SqlCommand(); 

      cmd1.CommandText = "insert into your_table values (@p,@p1,@p2)"; 

      cmd1.Parameters.Add("@p", SqlDbType.VarChar).Value = your_control.Text; 
      cmd1.Parameters.Add("@p1", SqlDbType.VarChar).Value = your_control.Text; 
      cmd1.Parameters.Add("@p2", SqlDbType.VarChar).Value = your_control.Text; 


      cmd1.CommandType = CommandType.Text; 


      cmd1.ExecuteNonQuery(); 

     } 
      this.sqlConnection1.Close(); 
0

你有一个sqlcommandbuilder,DataAdapter的,和DataTable申报?

例如:

SQLConnection con = (your connection); 
SQLDataAdapter = sda; 
SQLCommandBuilder = scb; 
DataTable = dt; 
private void btnEnter_Click(object sender, EventArgs e) 


da = new SqlDataAdapter("SELECT * FROM [table] WHERE [columnA]='" + txtData.Text + "' OR [columnB]='" + txtData.Text + "' OR [ColumnC]='" + txtData.Text + "' OR [ColumnD]='" + txtData.Text + "'", con); 
      ds = new DataSet(); 
      dt = new DataTable(); 
      ds.Clear(); 
      da.Fill(dt); 
      dg.DataSource = dt; 

      con.Open(); 
      con.Close(); 

private void btnUpdate_Click(object sender, EventArgs e) 
    { 
     //when button is clicked, the SQL Database gets updated with the data that is plugged into the datagridview. 
     scb = new SqlCommandBuilder(da); 
     da.Update(dt); 

    } 
相关问题