0

我已经阅读了所有与此有关的帖子,但无法弄清楚。我正在尝试从表单插入数据库,但是当我点击提交按钮并将 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=&quot;data source=xxxxxx;initial catalog=CateringAuthorization;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>

4

2 回答 2

2

查看 web.config 中的连接字符串,它包含单词 metadata。您的连接字符串应用作实体框架。看看使用实体框架来执行你的命令。

您可以创建一个EntityConnection

于 2013-08-31T21:34:25.383 回答
0

您正在使用 ADO.NET 连接 (SqlConnection),它不采用为实体框架指定的格式。

尝试“数据源=MY-PC\SQL2008;初始目录=CateringAuthorization;集成安全=True”

于 2013-08-31T21:41:57.590 回答