简体   繁体   English

如何将数据插入关系数据库

[英]How do I insert data into a relational database

I have 3 Microsoft SQL Server tables which consist of the following: 我有3个Microsoft SQL Server表,其中包含以下内容:

All 3 tables are related. 所有3个表都相关。

I need to insert data into the Training table which I can do. 我需要将数据插入“ Training表中,我可以这样做。

The problem is inserting the CustomerID column of the customers table into the training table by only knowing the customer name. 问题是仅通过了解客户名称将客户表的CustomerID列插入培训表中。

I created a separate class as I will be using this quite a lot in my applications. 我创建了一个单独的类,因为我将在我的应用程序中大量使用它。

 public void UpdateDatabase(string sql_query)
 {
        using (SqlConnection conn = new SqlConnection(connection_string))
        {
            using (SqlCommand comm = new SqlCommand(sql_query, conn))
            {
                conn.Open();
                comm.ExecuteNonQuery();
                conn.Close();
            }
        }
    }

This is my main class which is used to update my DB. 这是我的主类,用于更新数据库。

In my button click even i have the following code. 在我的按钮中单击,即使我有以下代码。

 private void btnSave_Click(object sender, EventArgs e)
 {
        DB NewJob = new DB();

        if (TabControl1.SelectedTab == TabControl1.TabPages["TPTraining"])//If TPTraiing Tab is selected
            {
                try
                {
                    string Customer = TPTcboCustomer.Text;
                    string Product = TPTcboProduct.Text;
                    DateTime DemoDate = TPTdtpDate.Value.Date;
                    string EndResult = TPTtxtEndResult.Text;

                    NewJob.UpdateDatabase("INSERT INTO TrainingDemos(CustomerID, ProductID, DemoDate, EndResult)VALUES('" + Customer + "','" + Product + "','" + DemoDate + "','" + EndResult + "')");
                    MessageBox.Show("Training/Demo Saved Sussesfully");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

The Customer variable holds the customer name that i get from the Customer Table. Customer变量保存了我从客户表中获得的客户名称。 Instead of inserting the Customer Name into the Training Table i must insert the CustomerID. 我必须插入客户ID,而不是将客户名称插入培训表中。

string Customer = TPTcboCustomer.Text;

When the form is loaded the TPTcboCustomer combobox control retrieves all customer names from the customer table. 加载表单后,TPTcboCustomer组合框控件将从客户表中检索所有客户名称。

Long story short, how do I insert the CustomerID into the training table by only knowing the customer name. 长话短说,如何仅通过了解客户名称将CustomerID插入培训表中。

You'll need to do another query to obtain the CustomerID. 您需要执行另一个查询来获取CustomerID。 Once you have that, you can do your insert. 一旦有了,就可以插入。

I mean, you need to verify that it's a real customer anyway, right? 我的意思是,您仍然需要验证它是真正的客户,对吗?

I usually prefer to fill a drop down (or similar field) with customers to choose from anyway when there's a finite number of items to choose from. 我通常更喜欢在客户数量有限的情况下,通过下拉列表(或类似字段)与客户进行选择。 Personally, I'd do an initial query pulling in all of the customers and populate the drop down with those customer objects. 就个人而言,我将进行初始查询以拉入所有客户,并使用这些客户对象填充下拉列表。 Then you just use the selected item's object information to do your insert. 然后,您只需使用所选项目的对象信息进行插入即可。

Make Customer a strong type: 使Customer成为强者:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    // Other properties as desired

    public override string ToString()
    {
        return Name ?? "<No Name>";
    }
}

Once you have that, read in all of your customers and populate the combo box with your list of Customer objects. 完成后,请阅读所有客户的信息,并在组合框中填充“客户”对象列表。 The ToString() will make it display the customer's name in the drop down. ToString()将使其在下拉列表中显示客户的姓名。 Then you just have to do: 然后,您只需要做:

Customer customer = (Customer)TPTcboCustomer.SelectedItem;

Then BLAM-O, you have all of the customer information you need to do your insert. 然后BLAM-O,您拥有插入所需的所有客户信息。

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

相关问题 如何在不单击“插入”按钮的情况下将数据插入数据库 - How do I insert data to a database without clicking the insert button 如何使用LINQ从关系数据库中获取对象? - How do I get an object from a relational database with LINQ? 如何从 C# 访问关系 SQL 数据库? - How do I access a relational SQL database from C#? 我如何为 RadioButton 和 CheckBox 编码以将数据插入数据库 - how do i code for RadioButton and CheckBox to insert Data into Database 如何将数据插入涉及C#中主键和外键的sql server关系数据库 - How to insert data to a sql server relational database that involve primary and foreign keys in C# 快速将关系(规范化)数据表插入SQL Server 2008数据库 - Fast insert relational(normalized) data tables into SQL Server 2008 database 如何将图像插入数据库? - How do I insert an image into a database? 如何在MySQL数据库中插入数据? - How I can Insert Data in the MySQL Database? 如何使用 Entity Framework .NET Core 将数据插入到我的数据库中? - How do I insert data into my database using Entity Framework .NET Core? 如何在Visual C#2008中使用sql将数据插入dbf格式的数据库中? - How do I insert data into a dbf format database using sql in Visual C# 2008?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM