使用访问数据库的动态元标记

时间:2012-10-24 06:54:45

标签: asp.net ms-access

我正在尝试根据此post从访问数据库生成动态元标记。

protected void Page_Load(object sender, EventArgs e)
{

    OleDbParameter spar = new OleDbParameter("@Title", Request["Title"]);
    string query = "SELECT * FROM storedata WHERE Title = @Title";
    OleDbConnection myconnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["accessConnectionString5"].ConnectionString);
    OleDbCommand SqlCmd = null;
    SqlCmd = new OleDbCommand(query, myconnection);

    SqlCmd.Parameters.Add(spar);

    SqlCmd.Connection.Open();
    SqlCmd.ExecuteNonQuery();


    OleDbDataAdapter ad = new OleDbDataAdapter(SqlCmd);
    DataTable dt = new DataTable();
    ad.Fill(dt);


    title.InnerHtml = dt.Rows[0]["Title"].ToString();
    keywords.Attributes.Add("content", dt.Rows[0]["Title"].ToString());


   // Closing the SQL connection
      SqlCmd.Connection.Close();

}

但我收到错误' System.Data.OleDb.OleDbException:参数@Title没有默认值'。

1 个答案:

答案 0 :(得分:0)

您说WHERE Title = @Title但我看不到您为此参数指定了值的位置。

SqlCmd = new OleDbCommand(query, myconnection); 
SqlCmd.Parameters.AddWithValue("@Title","a title");