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

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

本文介绍了如何从表中检索数据并插入另一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从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.

推荐答案


这篇关于如何从表中检索数据并插入另一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:45