如何在插入语句中使用选择子查询?

时间:2019-09-13 10:20:03

标签: c# sql-server

我正在尝试使用insert语句将一些值插入数据库。我还必须使用select语句从另一个表中获取与所选选项相对应的键。

我尝试了几个查询,但是都没有用。

    string query3 = "insert into students (FirstName, LastName, FatherName, 
Email, DateBirth, DateReg, Adress, Gender, Specialization, Country, 
Province, City) values ('" 
    + this.txt_fname.Text + "','" + this.txt_lname.Text + "','" 
    + this.txt_fathername.Text + "','" + this.txt_email.Text + "','" 
    + this.date_birth.Text + "', '" + this.date_reg.Text + "','" 
    + this.txt_adress.Text + "','" + this.Gender 
    + "', (select specialization_id from specialization where SpecializationName = '" + this.specialization.Text 
    + "'),
    (select country_id from country where CountryName ='" + this.comboBox2.Text 
    + "'),(select province_id from province where ProvinceName ='" 
              + this.comboBox4.Text 
    + "'),(select city_id from city where CityName ='"+ this.comboBox3.Text + "');";

我希望输出“保存”,但是我得到{“';'附近的语法不正确。”}

当我使用时:

'" + ("SELECT specialization_id from specialization where SpecializationName =" + this.specialization.Text)+ "' 

代替(上面写的):

(select specialization_id from specialization where SpecializationName = '" + this.specialization.Text + "')

我得到:

  

{“将varchar值'SELECT specialization_id从SpecializationName = Informatica Economica的专业转换为数据类型int时,转换失败。“

1 个答案:

答案 0 :(得分:1)

通常的警告,我不是C#程序员,我几乎不知道,但是我之前链接的文档足以让我正确地编写它:

string commandText = "INSERT INTO dbo.student (FirstName, LastName, FatherName, Email, DateBirth,DateReg, Adress, Gender, Specialization, Country, Province,City) " +
                     "SELECT @FirstName,@LastName, @Fathername, @Email, @DateBirth, @DateReg, @Address, @Gender, s.specialization_id, c.country_id, p.province_id, cy.city_id " +
                     "FROM (SELECT specialization_id FROM dbo.specialization WHERE SpecializationName = @Specialization) s " +
                     "CROSS APPLY (select country_id from country where CountryName = @Country) c " +
                     "CROSS APPLY (select province_id from province where ProvinceName = @Province) p " +
                     "CROSS APPLY (select city_id from city where CityName = @City) cy;";

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    command.Parameters.Add(@FirstName, SqlDbType.VarChar,50).Value = this.txt_fname.Text;
    command.Parameters.Add(@LastName, SqlDbType.VarChar,50).Value = this.txt_lname.Text;
    command.Parameters.Add(@Fathername, SqlDbType.VarChar,50).Value = this.txt_fathername.Text;
    command.Parameters.Add(@Email, SqlDbType.VarChar,50).Value = this.txt_email.Text;
    command.Parameters.Add(@DateBirth, SqlDbType.Date).Value = this.date_birth.Text; //Shouldn't this be a date picker object?
    command.Parameters.Add(@DateReg, SqlDbType.Date).Value = this.date_reg.Text; //Shouldn't this be a date picker object?
    command.Parameters.Add(@Address, SqlDbType.VarChar,200).Value = this.txt_adress.Text; //It's spelt Address (2 d's)
    command.Parameters.Add(@Gender, SqlDbType.VarChar,10).Value = this.Gender; //Why did this not have the Text property?
    command.Parameters.Add(@Specialization, SqlDbType.VarChar,50).Value = this.specialization.Text;
    command.Parameters.Add(@CountryName, SqlDbType.VarChar,50).Value = this.comboBox2.Text; //You should name this combo box
    command.Parameters.Add(@Province, SqlDbType.VarChar,50).Value = this.comboBox4.Text; //You should name this combo box
    command.Parameters.Add(@City, SqlDbType.VarChar,50).Value = this.comboBox3.Text;//You should name this combo box
}