本文介绍了DataGridView值不会插入到sql server中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private void btnPostSale_Click(object sender,EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
try
{
if(con.State == ConnectionState.Open)
{
con.Close();

if(con.State == ConnectionState.Closed)
{
string insert =" Insert into tblQuotationsLines(quoteID,stockID,stockDescription,stockQuantity,totalSellingPrice,totalAmount)值(@ quoteID,@ stockID,@ stockDescription,@ stockQuantity,@ totalSellingPrice,@总金额)英寸;

con.Open();

cmd = new SqlCommand(insert,con);




for(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
cmd.Parameters .Add(new SqlParameter(" @ quoteID",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockID",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockDescription",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockQuantity",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ totalSellingPrice",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ totalAmount",SqlDbType.VarChar));

cmd.Parameters [" @ quoteID"]。Value = dataGridView1.Rows [i] .Cells [" QuoteNumber"]。Value;

cmd.Parameters [" @ stockID"]。Value = dataGridView1.Rows [i] .Cells [" stockItems"]。Value;

cmd.Parameters [" @ stockDescription"]。Value = dataGridView1.Rows [i] .Cells [" itemDescription"]。Value;

cmd.Parameters [" @ stockQuantity"]。Value = dataGridView1.Rows [i] .Cells [" quantity"]。Value;

cmd.Parameters [" @ totalSellingPrice"]。Value = dataGridView1.Rows [i] .Cells [" sellingPrice"]。Value;

cmd.Parameters [" @ totalAmount"]。Value = dataGridView1.Rows [i] .Cells [" totalAmount"]。Value;
}

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show(" Quote post successfully");
}
其他
{

}
}
其他
{
//
string insert =" Insert into tblQuotationsLines(quoteID,stockID,stockDescription,stockQuantity,totalSellingPrice,totalAmount)值(@ quoteID,@ stockID,@ stockDescription,@ stockQuantity,@ totalSellingPrice,@ totalAmount)" ;;

con.Open();

cmd = new SqlCommand(insert,con);




for(int i = 0; i< dataGridView1.Rows.Count; i ++)
{
cmd.Parameters .Add(new SqlParameter(" @ quoteID",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockID",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockDescription",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ stockQuantity",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ totalSellingPrice",SqlDbType.VarChar));

cmd.Parameters.Add(new SqlParameter(" @ totalAmount",SqlDbType.VarChar));

cmd.Parameters [" @ quoteID"]。Value = dataGridView1.Rows [i] .Cells [" QuoteNumber"]。Value;

cmd.Parameters [" @ stockID"]。Value = dataGridView1.Rows [i] .Cells [" stockItems"]。Value;

cmd.Parameters [" @ stockDescription"]。Value = dataGridView1.Rows [i] .Cells [" itemDescription"]。Value;

cmd.Parameters [" @ stockQuantity"]。Value = dataGridView1.Rows [i] .Cells [" quantity"]。Value;

cmd.Parameters [" @ totalSellingPrice"]。Value = dataGridView1.Rows [i] .Cells [" sellingPrice"]。Value;

cmd.Parameters [" @ totalAmount"]。Value = dataGridView1.Rows [i] .Cells [" totalAmount"]。Value;
}

cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show(" Quote post successfully");
}
}
catch(例外)
{

//MessageBox.Show(ex.ToString());
}
Cursor.Current = Cursors.WaitCursor;
}




解决方案

private void btnPostSale_Click(object sender, EventArgs e)
         {
             Cursor.Current = Cursors.WaitCursor;
             try
             {
                 if (con.State == ConnectionState.Open)
                 {
                     con.Close();

                     if (con.State == ConnectionState.Closed)
                     {
                         string insert = "Insert Into tblQuotationsLines(quoteID,stockID,stockDescription,stockQuantity,totalSellingPrice,totalAmount) Values (@quoteID,@stockID,@stockDescription,@stockQuantity,@totalSellingPrice,@totalAmount)";

                         con.Open();

                         cmd = new SqlCommand(insert, con);




                         for (int i = 0; i < dataGridView1.Rows.Count; i++)
                         {
                             cmd.Parameters.Add(new SqlParameter("@quoteID", SqlDbType.VarChar));

                             cmd.Parameters.Add(new SqlParameter("@stockID", SqlDbType.VarChar));

                             cmd.Parameters.Add(new SqlParameter("@stockDescription", SqlDbType.VarChar));

                             cmd.Parameters.Add(new SqlParameter("@stockQuantity", SqlDbType.VarChar));

                             cmd.Parameters.Add(new SqlParameter("@totalSellingPrice", SqlDbType.VarChar));

                             cmd.Parameters.Add(new SqlParameter("@totalAmount", SqlDbType.VarChar));

                             cmd.Parameters["@quoteID"].Value = dataGridView1.Rows[i].Cells["QuoteNumber"].Value;

                             cmd.Parameters["@stockID"].Value = dataGridView1.Rows[i].Cells["stockItems"].Value;

                             cmd.Parameters["@stockDescription"].Value = dataGridView1.Rows[i].Cells["itemDescription"].Value;

                             cmd.Parameters["@stockQuantity"].Value = dataGridView1.Rows[i].Cells["quantity"].Value;

                             cmd.Parameters["@totalSellingPrice"].Value = dataGridView1.Rows[i].Cells["sellingPrice"].Value;

                             cmd.Parameters["@totalAmount"].Value = dataGridView1.Rows[i].Cells["totalAmount"].Value;
                         }

                         cmd.ExecuteNonQuery();

                         con.Close();

                         MessageBox.Show("Quote post successfully");
                     }
                     else
                     {

                     }
                 }
                 else
                 {
                     //
                     string insert = "Insert Into tblQuotationsLines(quoteID,stockID,stockDescription,stockQuantity,totalSellingPrice,totalAmount) Values (@quoteID,@stockID,@stockDescription,@stockQuantity,@totalSellingPrice,@totalAmount)";

                     con.Open();

                     cmd = new SqlCommand(insert, con);




                     for (int i = 0; i < dataGridView1.Rows.Count; i++)
                     {
                         cmd.Parameters.Add(new SqlParameter("@quoteID", SqlDbType.VarChar));

                         cmd.Parameters.Add(new SqlParameter("@stockID", SqlDbType.VarChar));

                         cmd.Parameters.Add(new SqlParameter("@stockDescription", SqlDbType.VarChar));

                         cmd.Parameters.Add(new SqlParameter("@stockQuantity", SqlDbType.VarChar));

                         cmd.Parameters.Add(new SqlParameter("@totalSellingPrice", SqlDbType.VarChar));

                         cmd.Parameters.Add(new SqlParameter("@totalAmount", SqlDbType.VarChar));

                         cmd.Parameters["@quoteID"].Value = dataGridView1.Rows[i].Cells["QuoteNumber"].Value;

                         cmd.Parameters["@stockID"].Value = dataGridView1.Rows[i].Cells["stockItems"].Value;

                         cmd.Parameters["@stockDescription"].Value = dataGridView1.Rows[i].Cells["itemDescription"].Value;

                         cmd.Parameters["@stockQuantity"].Value = dataGridView1.Rows[i].Cells["quantity"].Value;

                         cmd.Parameters["@totalSellingPrice"].Value = dataGridView1.Rows[i].Cells["sellingPrice"].Value;

                         cmd.Parameters["@totalAmount"].Value = dataGridView1.Rows[i].Cells["totalAmount"].Value;
                     }

                     cmd.ExecuteNonQuery();

                     con.Close();

                     MessageBox.Show("Quote post successfully");
                 }
             }
             catch (Exception )
             {

                 //MessageBox.Show(ex.ToString());
             }
             Cursor.Current = Cursors.WaitCursor;
         }


解决方案


这篇关于DataGridView值不会插入到sql server中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 19:55