如何从表中检索数据并插入另一个表?

问题描述:

我正在尝试从Customer表中检索customer_id并将其插入



I am trying to retrieve "customer_id" from Customer table and insert it into

fare_tariff(tariff_id, customer_id, total_price)





所以我从Customer表中检索customer_id如下:





So I retrieve the customer_id from Customer table as below:

using (SqlCommand command = new SqlCommand("SELECT customer_id FROM Customer WHERE UserName = '" + username + "' Password = '"+password +"' ", connection))
                {
                    string cust_id = customer_id.ToString();
                    SqlDataReader myReader = command.ExecuteReader();

                     if (myReader.Read())
                        {
                            cust_id = myReader["customer_id"].ToString();
                        }

                    int c_id = Convert.ToInt32(cust_id);

                   myReader.Close();
                   custID(c_id);
                }





并将customer_id插入表格fare_tariff,如下所示:





and insert the customer_id into table fare_tariff like below:

using (SqlCommand command = new SqlCommand("INSERT INTO flight_reservation(tariff_id, customer_id, total_price) VALUES(@val1,@val2,@val3)", connection))
                {

                    command.Parameters.Add("@val1", SqlDbType.Int).Value = tariff_id;
                    command.Parameters.Add("@val2", SqlDbType.Int).Value = customer_id;
                    command.Parameters.Add("@val3", SqlDbType.VarChar).Value = total_price.ToString();

                    command.ExecuteNonQuery();
                }









我将customer_id声明为变量用于存储customer_id。



问题是:tariff_id和total_price已成功插入,但是customer_id列尚未生效。



需要帮助。





I declared customer_id as a variable for storing customer_id.

Problem is: tariff_id and total_price inserted successfully but the column customer_id is null yet.

Help needed.

*调试程序,确保值正确传递。

*使用SQL Server Profiler检查DB的内容
* Debug the program, make sure the value is getting passed correctly.
* Use SQL Server Profiler to check what is going to the DB