2012-07-18 32 views
0

我又回来了。 我之前曾问过一个类似的问题,但即使在前一个anwser的帮助下,并用questionmarks尝试它,而不是添加我尝试过AddWithValue,我没有任何运气。SQL ASP.NET INSERT麻烦(第2部分)

我试图将txt_Naam更改为txt_Naam.Text,什么都没有。

同时把[]围绕在列名上,没有运气。 它不断给我这个“INSERT INTO语句中的语法错误。”。

这次我没有得到下面的代码。 可能是小事,但我无法弄清楚。 (再次...)

 protected void btn_final_Click(object sender, EventArgs e) 
    { 

     string fact_adres = txt_Naam.Text + "," + txt_Anaam.Text + "," + txt_Adres.Text + "," + txt_Toevoeg.Text + "," + txt_Pcode.Text + "," + txt_Plaats.Text + "," + txt_Email.Text ; 
     string fact_adres1 = txt_Naam1.Text + "," + txt_Anaam1.Text + "," + txt_Adres1.Text + "," + txt_Toevoeg1.Text + "," + txt_Pcode1.Text + "," + txt_Plaats1.Text + "," + txt_Email1.Text; 

     string a = "1"; 

     OleDbConnection conn = new OleDbConnection(); 
     conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; " 
      + "Data Source=|DataDirectory|webwinkel.accdb"; 

     OleDbCommand cmd = new OleDbCommand(); 
     cmd.Connection = conn; 
     cmd.CommandText = "INSERT INTO Order (factuur_adres_id, verzend_adres_id, totaalprijs) VALUES (?, ?, ?);"; 

     cmd.Parameters.Add("@factuur_adres", OleDbType.VarChar, 125).Value = fact_adres; 
     cmd.Parameters.Add("@verzend_adres", OleDbType.VarChar, 125).Value = fact_adres1; 
     cmd.Parameters.Add("@totaal_prijs", OleDbType.VarChar, 7).Value = a; 

     try 
     { 
      conn.Open(); 
      OleDbDataReader reader = cmd.ExecuteReader(); 
      reader.Close(); 
     } 

     catch (Exception exc) 
     { 
      Label1.Text = exc.Message; 
     } 

     finally 
     { 
      conn.Close(); 
      Session["Winkelwagen"] = null; 
     } 

    } 

回答

2

您的命令文本应该

cmd.CommandText = "INSERT INTO Order (factuur_adres_id, verzend_adres_id, totaalprijs) VALUES (@factuur_adres,@verzend_adres, @totaal_prijs)"; 

更新答案: 与运行参数设置你的代码,直接传递价值,并确认其工作或不

cmd.CommandText = "INSERT INTO Order (factuur_adres_id, verzend_adres_id, totaalprijs) VALUES ('abc','def','adf')"; 
+0

我试过了,它仍然给我错误。这就是为什么我将它改为questionmarks。 – 2012-07-18 21:03:38

+0

@ Big.S你现在得到了什么错误? – 2012-07-18 21:04:21

+0

@ Big.S在你的sql服务器上运行上面的查询并检查它是否在那里工作?语法是正确的。 – 2012-07-18 21:08:44

0

当您插入时,您不必使用

cmd.ExecuteNonQ uery()

而不是cmd.ExecuteReader()?