简体   繁体   English

如何在C#中将DataGridView的数据引用到SQL数据库中?

[英]How can I reference data of DataGridView into SQL database in C#?

I am new to database and C#. 我是数据库和C#的新手。 I am building an EPOS software and I just want to reference the data which is in DataGridView into SQL database (if I am saying it correct). 我正在构建一个EPOS软件,我只想将DataGridView中的数据引用到SQL数据库中(如果我说正确的话)。 Ok, here is what I am trying to do: 好的,这是我想要做的:

as shown in the photo, I have customer1 order details (3 rows) in DataGridView and this details is only for one customer1. 如图所示,我在DataGridView中有customer1订单详细信息(3行),该详细信息仅适用于一个customer1。 Now when I finish with customer1, its order details is saved in database. 现在,当我完成对customer1的操作后,其订单详细信息将保存在数据库中。 Next, when I finish with customer2 order, its details is saved in database. 接下来,当我完成customer2订单时,其详细信息将保存在数据库中。 Next, customer3 order details will be saved in database.... and so on. 接下来,customer3订单详细信息将保存在数据库中。。。等等。

Now for example at some point in the future I want to read back / Edit (or check) customer2 order details from database and populate it into DataGridView. 现在,例如,在将来的某个时刻,我想从数据库中读取/编辑(或检查)customer2订单详细信息,并将其填充到DataGridView中。

I have been thinking how to do it but I failed and I am confused. 我一直在想怎么做,但我失败了,我感到困惑。 Can you please provide me with an idea or code in how to check what did Customer2 order. 您能为我提供有关如何检查Customer2订购什么的想法或代码。 thank you. 谢谢。

照片

Create a Customer Model which represent your table. 创建一个代表您的表的Customer模型。 Then create a method which will return a collection. 然后创建一个将返回集合的方法。 I'm going to assume you've got your connection string setup and know how to read it in your code using c#. 我将假设您已经设置了连接字符串,并且知道如何使用c#在代码中读取它。 So the code could look like: 因此,代码可能类似于:

public class Customer
{
    public int Quantity { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
}

public List<Customer> GetCustomerData()
{
  var list = new List<Customer>();

  string sqlQuery = "SELECT Qty, Des, Price From MyTable Where Customer = SomeCustomer"; //Change accordingly
  string connectionString = "Your Connection String";

  using (var connection = new SqlConnection(connectionString))
  {
    using (var cmd = new SqlCommand(sqlQuery, connection))
    {
      connection.Open();

       using (var reader = cmd.ExecuteReader())
       {
         while (reader.Read())
         {
            var c = new Customer();
            c.Quantity = Convert.ToInt32(reader["Qty"]);
            c.Description = reader["Des"].ToString();
            c.Price = Convert.ToDouble(reader["Price"]);

            list.Add(c);
          }
       }
     }
   }
   return list;
}

Finnaly to display the data on the DataGrid you'll do: 最后要在DataGrid上显示数据,您将执行以下操作:

MyGrid.ItemsSource = GetCustomerData(); //Change MyGrid to whatever you've named your grid

Please note: I haven't tested the above code so it might require some small changes. 请注意:我尚未测试上面的代码,因此可能需要一些小的更改。 However it should give you a good idea. 但是,它应该给您一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM