DataGridView值不会插入到sql server中 [英] DataGridView values wont insert into sql server

查看:52
本文介绍了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;
}





解决方案

您好,


发布代码信息不足。请详细说明,尽可能多地提供详细信息,例如:是基于数据库服务器还是.MDF可能在特定文件夹中。如果代码在没有抛出异常的情况下执行(假设在一个点上的try-catch
,MessageBox内部没有被注释掉)它是什么,如果没有抛出异常,你需要检查ExecuteNonQuery的返回值,这是一个int。


以后在发布代码时总是使用代码块。



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


解决方案

Hello,

Posting code is not enough information. Please elaborate, provide as many details as possible e.g. is the database server based or a .MDF perhaps in a specific folder. If the code executes without throwing an exception (assuming the try-catch at one point the MessageBox within was not commented out) what is it and if no exception was thrown, you need to check the return value from ExecuteNonQuery which is an int.

In the future always use a code block when posting code.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆