簡體   English   中英

如何在插入語句中使用選擇子查詢?

[英]How to use select sub Query in insert statement?

我正在嘗試使用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時,轉換失敗。“}

我通常的警告是,我不是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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM