2017-04-17 93 views
-1

enter image description here關鍵字錯誤

public string ss = "Data Source=D\\SQLEXPRESS;Initial Catalog=gym;Integrated Security=True"; 

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     string q2 = "insert into database.gym (name,weight,height,add_class,gender,fees) values('" + this.textBox1.Text + "','" + this.textBox2.Text + "','" + this.textBox3.Text + "','" + this.comboBox1.Text + "','" + this.comboBox2.Text + "','" + this.comboBox3.Text + " ') ;"; 
     SqlConnection con = new SqlConnection(ss); 
     SqlCommand cmd = new SqlCommand(q2, con); 
     SqlDataReader read; 
     try 
     { 
      con.Open(); 
      read = cmd.ExecuteReader(); 
      MessageBox.Show("Welcome to our gym"); 
      while (read.Read()) { }; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
+0

什麼是在那些查詢數據庫? – Ariwibawa

+1

'數據庫'是'Sql Server'中的一個* reserved *關鍵字。你真的有名爲'Database'的模式嗎? –

+0

從連接字符串中,數據庫名稱是健身房。對於插入查詢,正確的語法是** databasename.schema.tablename **。您的命令需要'INSERT INTO gym.schema.table' – Raj

回答

0
insert into database.gym 

取而代之的是使用一個有效的表名

insert into Table 

正確的語法爲[Database].[Schema].[TableName]

+0

出現錯誤(「無效的對象名稱gym.schema.customer」)..我該怎麼辦? @saurabh –

+0

你不應該使用word schema,請嘗試gym.dbo.customer。用dbo替換架構 – Saurabh

+0

我也嘗試了gym.dbo.customer,但另一個錯誤出現「無效對象名稱gym.dbo.customer」 –