当我尝试将数据插入数据库时,会出现此错误
System.Data.SqlClient.Sqlexception(0x80131904):System.Data.SqlClient.SqlConnection中的“Name”附近语法不正确。
System.Data.SqlClient.SqlInternalConnection.OnError(SqlCeption exception,Boolean breakConnection,Action1WrapCloseInAction)位于System.Data.SqlClient.TdSparser.ThrowExceptionandWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)位于System.Data.SqlClient.TdSparser.TryRun(RunBehavior RunBehavior,SqlCommand cmdHandler,havior runBehavior,Boolean returnStream,String方法,TaskCompletionSource1完成,Int32超时,Task&;c:\users\IBTISAM TANVEER\Documents\Visual Studio 2012\website1\AddUser.aspx.cs:第53行ClientConnectionID:DF4AEC92-1F96-4236-9BD7-F802A52B5213错误号:102,状态:1,类:15
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class adduser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
SqlConnection conec = new SqlConnection(ConfigurationManager.ConnectionStrings["DonorInformationConnectionString"].ConnectionString);
conec.Open();
string checkuserCNIC = "select count(*) from Donor where CNIC='" + TextBoxCNIC.Text + "'";
SqlCommand com = new SqlCommand(checkuserCNIC,conec);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Response.Write("User Already Exists");
}
conec.Close();
}
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conec = new SqlConnection(ConfigurationManager.ConnectionStrings["DonorInformationConnectionString"].ConnectionString);
conec.Open();
string insertquerry = "insert into Donor(First Name, Last Name, Cell number, Email, CNIC, City, Address, Blood Group, Gender, Password) values (@firstname, @lastname, @cell, @email, @cnic, @city, @address, @blood, @sex, @password)";
SqlCommand com = new SqlCommand(insertquerry, conec);
com.Parameters.AddWithValue("@firstname", TextBoxFirst_Name.Text);
com.Parameters.AddWithValue("@lastname", TextBoxLast_Name.Text);
com.Parameters.AddWithValue("@cell", TextBoxPhone.Text);
com.Parameters.AddWithValue("@email", TextBox_Email.Text);
com.Parameters.AddWithValue("@cnic", TextBoxCNIC.Text);
com.Parameters.AddWithValue("@city", DropDownList_City.SelectedItem.ToString());
com.Parameters.AddWithValue("address", TextBox_Address.Text);
com.Parameters.AddWithValue("@blood", DropDownListBloodGroup.SelectedItem.ToString());
com.Parameters.AddWithValue("@sex", DropDownList_Gender.SelectedItem.ToString());
com.Parameters.AddWithValue("@password", TextBoxCNIC.Text);
com.ExecuteNonQuery();
Response.Write("Donor Added Successfully");
conec.Close();
}
catch(Exception ex)
{
Response.Write("There is some Errors Please Read------------------>" + ex.ToString());
}
}
protected void DropDownList_Gender_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
使用此First Name
与[First Name]
和其他列一样。
如果列名中有空格,则应始终使用[]
。此外,您应该避免列名中的空格。
所以您的代码变成
string insertquerry = "insert into Donor([First Name], [Last Name],
[Cell number], Email, CNIC, City, Address, Blood Group, Gender,
Password) values (@firstname, @lastname, @cell, @email, @cnic, @city,
@address, @blood, @sex, @password)";