2017-06-01 45 views
-1

大家好,這是我在爲了從一個簡單的形式獲取數據,並將其導入到我的數據庫編寫的代碼:SQL異常:列名或提供值的數目不匹配表定義

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Data; 

public partial class Default2 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click1(object sender, EventArgs e) 
    { 
     SqlConnection connection = new SqlConnection("Data Source=ICSD-DB\\ICSDMSSQLSRV;Initial Catalog=icsd15005;Integrated Security=True"); 
     //1os tropos 
     String sqlString = "INSERT INTO pr_foititis VALUES(@arithmos_taut,@onoma,@eponymo, @imerominia_proslipsis,@imerominia_gennisis,@misthos)"; 
     //2os tropos mh asfales, Sql injection, gia na to ektelesoume afairoume apo kato tis entoles pou ksekinoun me command.Parameters.Add 
     //String sqlString = "INSERT INTO pr_foititis VALUES('"+id_Tb.Text+"','"+name_Tb.Text;+"','"+surname_Tb.Text;+"', "+age_Tb.Text;+")"; 

     try 
     { 
      connection.Open(); 
      SqlCommand command = new SqlCommand(sqlString, connection); 
      command.Parameters.Add("@arithmos_taut", SqlDbType.Int).Value = TextBox1.Text; 
      command.Parameters.Add("@onoma", SqlDbType.VarChar).Value = TextBox2.Text; 
      command.Parameters.Add("@eponymo", SqlDbType.VarChar).Value = TextBox3.Text; 
      command.Parameters.Add("@imerominia_proslipsis", SqlDbType.Date).Value = TextBox4.Text; 
      command.Parameters.Add("@imerominia_gennisis", SqlDbType.Date).Value = TextBox5.Text; 
      command.Parameters.Add("@misthos", SqlDbType.Float).Value = TextBox6.Text; 
      command.ExecuteNonQuery(); 
      connection.Close(); 
      resultLabel.Text = "All Good!"; 
     } 
     catch (Exception ex) 
     { 
      resultLabel.Text = ex.ToString(); 
     } 
    } 
} 

這裏是例外:

System.Data.SqlClient.SqlException(0x80131904):提供的值的列名或數量與表定義不匹配。 (35行)

+0

請閱讀[問]並寫出正確的標題。 「數據庫錯誤」不夠清楚並且標題應該從標題中避免 –

+0

還要讀取和編輯您的問題以包含[mcve],但沒有表定義我們不能幫助您 –

+0

嘗試添加在插入中設置的列INSERT INTO pr_foititis COL1,COL2)VALUES(FOO,BAR) – hardkoded

回答

1

除非你在INSERT語句中有值的數量(並序)在表正是匹配列的數量和順序,你需要定義列。

String sqlString = "INSERT INTO pr_foititis (ColumnA, ColumnB, ColumnC, ColumnD, ColumnE, ColumnF) VALUES (...)" 
0

您會收到此錯誤消息,因爲您的表格內容比您提供的值多。

只需交叉檢查您的表格列或使用下面的代碼。

String sqlQuery = "INSERT INTO pr_foititis (Column1, Column2, Column3, Column4) VALUES (Value1,Value2,Value3,Value4)" 

希望這會幫助你。

謝謝!

相關問題