我已经阅读了所有与此有关的帖子,但无法弄清楚。我正在尝试从表单插入数据库,但是当我点击提交按钮并将 BookingName 传递给后面的代码时,我得到“不支持关键字:'元数据'”。我想我的问题出在 web.config 中。任何帮助将不胜感激。仍然掌握 .NET 的窍门
这是错误:
说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息: System.ArgumentException:不支持关键字:“元数据”。
源错误:
Line 20: {
Line 21: string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CateringAuthorizationEntities"].ConnectionString;
Line 22: SqlConnection conn = new SqlConnection(connString);
Line 23: string sql = "INSERT INTO tbBooking (BookingName) VALUES "
Line 24: + " (@BookingName)";
C#代码:
private void ExecuteInsert(string name)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CateringAuthorizationEntities"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
string sql = "INSERT INTO tbBooking (BookingName) VALUES "
+ " (@BookingName)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[1];
//param[0] = new SqlParameter("@id", SqlDbType.Int, 20);
param[0] = new SqlParameter("@BookingName", System.Data.SqlDbType.VarChar, 50);
param[0].Value = name;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void BtnCatering_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
//call the method to execute insert to the database
ExecuteInsert(BookingName.Text);
Response.Write("Record was successfully added!");
}
}
网络配置:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
<add name="CateringAuthorizationEntities" connectionString="metadata=res://*/App_Code.CateringAuthorization.csdl|res://*/App_Code.CateringAuthorization.ssdl|res://*/App_Code.CateringAuthorization.msl;provider=System.Data.SqlClient;provider connection string="data source=xxxxxx;initial catalog=CateringAuthorization;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>