C# SQL Server错误&x27;不支持关键字';数据源';
我目前正在尝试将一些值插入数据源,然后使用DataList控件在另一个页面上显示它们。然而,经过一些测试和实验,我发现错误来自一开始 这是我绑定到按钮的代码C# SQL Server错误&x27;不支持关键字';数据源';,c#,asp.net,database,sql-server-2008,datasource,C#,Asp.net,Database,Sql Server 2008,Datasource,我目前正在尝试将一些值插入数据源,然后使用DataList控件在另一个页面上显示它们。然而,经过一些测试和实验,我发现错误来自一开始 这是我绑定到按钮的代码 protected void btnSend_Click(object sender, EventArgs e) { Page.Validate("vld2"); SendMail(); lblMsgSend.Visible = true; txtPhone.Text = ""; txtEma
protected void btnSend_Click(object sender, EventArgs e)
{
Page.Validate("vld2");
SendMail();
lblMsgSend.Visible = true;
txtPhone.Text = "";
txtEmail.Text = "";
txtName.Text = "";
txtComment.Text = "";
//SQL Server Database
SqlConnection conn; //manages connection to database
SqlCommand cmd; //manages the SQL statements
string strInsert; //SQL INSERT Statement
try
{
//create a connection object
conn = new SqlConnection("DataSource=localhost\\sqlexpress;" +
"Initial Catalog=RionServer;" +
"Integrated Security=True;");
//Build the SQL INSERT Document
strInsert = "INSERT INTO CommentsAdmin (Name,Phone,Email,Comments)"
+ "VALUES(@Name,@Phone,@Email,@Comments);";
//associate the INSERT statement with the connection
cmd = new SqlCommand(strInsert, conn);
//TELL the SqlCommand WHERE to get the data from
cmd.Parameters.AddWithValue("Name", txtName);
cmd.Parameters.AddWithValue("Phone", txtPhone);
cmd.Parameters.AddWithValue("Email", txtEmail);
cmd.Parameters.AddWithValue("Comments", txtComment);
//open the connection
cmd.Connection.Open();
//run the SQL statement
cmd.ExecuteNonQuery();
//close connection
cmd.Connection.Close();
//display status message on the webpage
lblMsgSend.Text = "Thank you for the comment! Please hit the 'Return to Main Page' to return to the Main Page!";
}
catch (Exception ex)
{
lblMsgSend.Text = ex.Message;
}
}
这是我的网页图像和显示的错误
如果您需要更多信息,请告诉我
提前感谢。在您的连接字符串中,它应该是“数据源”,而不是“数据源”。只需添加一个空格。cmd.Parameters.AddWithValue(“@Name”,txtName);。我想你错过了@-1史瑞你在说什么。。OP在他的标题中清楚地说明了他的问题。。不确定你从哪里想出了一些他在代码中明确包含的东西
关键字DataSource
不支持空格清楚地解决了他的问题..好吧,这样就行了。然而,我有一个新的错误。“对象类型System.Web.UI.WebControl.TextBox与已知的托管提供程序本机类型之间不存在映射。”从文本框设置参数值时,应使用.Text
属性。如下所示:cmd.Parameters.AddWithValue(“Name”,txtName.Text)哈,这就是我在工作中的愚蠢!这样的小细节遗漏了。谢谢你的帮助!