-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行)
請閱讀[問]並寫出正確的標題。 「數據庫錯誤」不夠清楚並且標題應該從標題中避免 –
還要讀取和編輯您的問題以包含[mcve],但沒有表定義我們不能幫助您 –
嘗試添加在插入中設置的列INSERT INTO pr_foititis COL1,COL2)VALUES(FOO,BAR) – hardkoded