当我尝试将数据插入数据库
时出现此错误System.Data.SqlClient.SqlException(0x80131904): System.Data.SqlClient.SqlConnection附近的名称不正确。
OnError(SqlException异常, 布尔值breakConnection,Action1 wrapCloseInAction)at System.Data.SqlClient.SqlInternalConnection.OnError(SQLEXCEPTION exception,Boolean breakConnection,Action1 wrapCloseInAction)at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,布尔& dataReady)at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior,String resetOptionsString)at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32超时,任务& task,Boolean asyncWrite,SqlDataReader ds)at System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource1完成,Int32超时,任务&任务, 布尔asyncWrite)at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion,String methodName,Boolean sendToPipe,Int32 timeout, 布尔asyncWrite)at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()at adduser.Button1_Click(Object sender,EventArgs e)在c:\ Users \ Ibtisam中 Tanveer \ Documents \ Visual Studio 2012 \ WebSites \ 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)
{
}
}
答案 0 :(得分:3)
使用此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)";