我已阅读所有与此有关的帖子,但无法弄明白。我正在尝试从表单插入数据库但是当我点击提交按钮并将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!");
}
}
Web.Config中:
<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"/>
答案 0 :(得分:2)
查看web.config中的连接字符串,它包含单词metadata。您的连接字符串应该用作实体框架。查看使用Entity Framework执行命令。
您可以创建EntityConnection
答案 1 :(得分:0)
您正在使用ADO.NET连接(SqlConnection),它不采用为Entity Framework指定的格式。
尝试 “data source = MY-PC \ SQL2008; initial catalog = CateringAuthorization; integrated security = True”