同时将datagridview和textbox值插入到访问表中 [英] Insert datagridview and textbox values into access table at same time

查看:79
本文介绍了同时将datagridview和textbox值插入到访问表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
iam创建一个项目,其中有4个文本框和一个datagridview,在datagridview中有7行现在我的查询是可能只有一个事件,如按钮事件datagridview多个数据和文本框数据可以插入?我被尝试但没有成功我这就是为什么iam现在以单独的方式插入数据



我尝试过:



这里是我的文本框数据保存在数据库代码中,写在按钮事件上

Hi all
iam creating one project where having 4 textboxes and one datagridview, in datagridview having 7 rows now my query is is that possible only one event like button event datagridview multiple data and textbox data can insert? i was tried but not successful im so that's why iam now inserting data by separate way

What I have tried:

here is my textbox data save in database code which written on button event

string connectionString = null;
           connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
           con.ConnectionString = connectionString;

           string SqlString = "insert into Customer_info ([Date],[Name],[Doctor_name],[Address]) values(?,?,?,?)";
           using (cmd = new OleDbCommand(SqlString, con))
           {

               con.Open();
               cmd.CommandType = CommandType.Text;

               cmd.Parameters.AddWithValue("@Date", DateTime.Parse(msktextdate.Text));
               cmd.Parameters.AddWithValue("@Name", txtname.Text);
               cmd.Parameters.AddWithValue("@Doctor_name", txtdoctorname.Text);
               cmd.Parameters.AddWithValue("@Address", txtaddress.Text);




               int n = cmd.ExecuteNonQuery();
               con.Close();
               if (n > 0)
               {
                   MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
               }





以下代码是datagridview数据插入数据库写入dataGridView1_RowLeave事件



below code is datagridview data insert into database written on dataGridView1_RowLeave event

if (dataGridView1.IsCurrentRowDirty)
              {
                  string connectionString = null;
                  connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
                  con.ConnectionString = connectionString;
                  string cmd1 = "insert into Medicine_Detail ([Quantity],[Medicine_name],[Medicine_cost],[Manufacture_Name],[Batch_No],[Expiry_Date],[Rupees],[Total]) values(?,?,?,?,?,?,?,?)";
                  OleDbCommand cmd = new OleDbCommand(cmd1, con);
                  cmd.CommandType = CommandType.Text;

                  string Quantity = dataGridView1.Rows[e.RowIndex].Cells["Quantity"].Value.ToString();
                  cmd.Parameters.AddWithValue("@Quantity", Quantity);

                  string Medicine_name = dataGridView1.Rows[e.RowIndex].Cells["Medicine_name"].Value.ToString();
                  cmd.Parameters.AddWithValue("@Medicine_name", Medicine_name);

                  int Medicine_cost;
                  bool Medicine_cosHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Medicine_cost"].Value.ToString(), out Medicine_cost);
                  cmd.Parameters.AddWithValue("@Medicine_cost", Medicine_cost);

                  string Batch_No = dataGridView1.Rows[e.RowIndex].Cells["Batch_No"].Value.ToString();
                  cmd.Parameters.AddWithValue("@Batch_No", Batch_No);

                  string Manufacture_Name = dataGridView1.Rows[e.RowIndex].Cells["Manufacture_Name"].Value.ToString();
                  cmd.Parameters.AddWithValue("@Manufacture_Name", Manufacture_Name);

                  DateTime datetime;
                  bool dateTimeHasValue = DateTime.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Expiry_Date"].Value.ToString(), out datetime);
                  cmd.Parameters.AddWithValue("@Expiry_Date", datetime);

                  int Rupees;
                  bool RupeesHasValue = Int32.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Rupees"].Value.ToString(), out Rupees);
                  cmd.Parameters.AddWithValue("@Rupees", Rupees);

                  cmd.Parameters.AddWithValue("@Total", Convert.ToDouble(label9.Text));

                  con.Open();
                  int n = cmd.ExecuteNonQuery();
                  con.Close();

                  if (n > 0)
                  {


                      MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);


                  }





i希望这两个不同的插入同时在同一时间event



i want this two different insertion at same time at same event

推荐答案

不确定你的意思是我被尝试但没有成功 - 这是什么意思/看起来像? - 你有什么/没有进入你的数据库



我会说你最大的问题是数据库架构/一致性而不是插入数据 - 在面对它,customer_info和medicine_detail表是不相交的,即它们之间没有关系



我会像Rick建议的那样做,并且在交易中,做



Update_CustomerInfo()

Update_MedicineDetail()



(例如),确保表格确实应该是它们之间的关系。



我看到的另一个问题是,您似乎正在使用两个不同的事件来触发插入...不确定我喜欢这个想法 - 使用一个事件,或者,甚至是保存按钮 - 您可以轻松地检查/定位当前网格行。
Not sure what you mean by "i was tried but not successful" - what does that mean/look like ? - what are you getting/not getting in your database

I'd say you're biggest issue was database schema/consistency rather than the inserting of data - on the face of it, the customer_info and medicine_detail tables are disjoint ie no relationship between them

I would do as Rick suggested, and in the transaction, do

Update_CustomerInfo()
Update_MedicineDetail()

(for example), ensuring consistency between the tables if indeed there is supposed to be a relationship between them.

The other issue I see, is you seem to be using two different events to trigger the inserts ... not sure I like that idea - use one event, or, even a 'save' button - you can check/locate the current grid row easily.


也许您可以使用事务,它可以在Access中使用。 />
此处提供更多信息: c# - 我们是否在MS中进行交易-访问? - 堆栈溢出 [ ^ ]



另外看一下 System.Transactions 命名空间,这里是一个例如:

All About TransactionScope [ ^ ]
Maybe you could use transactions, it's possible in Access.
More information here: c# - Do we have transactions in MS-Access? - Stack Overflow[^]

Also take a look at the System.Transactions namespace, here is an example:
All About TransactionScope[^]


这篇关于同时将datagridview和textbox值插入到访问表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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