0

我正在关注一个会话教程。问题是这部分。

  OleDbCommand fecth =  new OleDbCommand(
     "SELECT * FROM YONETICI  Where  YNAME'" + 
      txtad.Text + "' and YPASS'" + txtpass.Text + "' ", 
     con);

在这部分,我收到一个名为 Incorrect syntax -Missing operator 的异常(我已尝试翻译)

这是剩下的代码

 OleDbConnection con = new OleDbConnection(
         "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+
         Server.MapPath("App_Data\\db.accdb"));
    con.Open();



    OleDbCommand fecth =  new OleDbCommand(
           "SELECT * FROM YONETICI  Where  YNAME'" + 
            txtad.Text + "' and YPASS'" + txtpass.Text + "' ", 
            con);
   OleDbDataReader dr=fecth.ExecuteReader();
    if(dr.Read()){
    Session.Add("value",txtad.Text);
    Response.Redirect("Default.aspx");
4

2 回答 2

2

确切地说,您需要添加 2 个等号,但我更喜欢以更好的方式编写您的查询,这将用下面的代码替换 @Parameter

fetch.Parameters.addWithValue()


OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+Server.MapPath("App_Data\\db.accdb"));
con.Open();

OleDbCommand fecth =  new OleDbCommand("SELECT * FROM YONETICI  Where  YNAME='@txtad' and YPASS='@txtpass'", con);

fecth.Parameters.AddWithValue("@txtad",txtad.Text);
fecth.Parameters.AddWithValue("@txtpass",txtpass.Text);
OleDbDataReader dr=fecth.ExecuteReader();
if(dr.Read()){
Session.Add("value",txtad.Text);
Response.Redirect("Default.aspx");
于 2013-08-30T18:52:35.137 回答
2

你需要一个等号运算符。

OleDbCommand fecth =  new OleDbCommand(
                          "SELECT * FROM YONETICI  Where YNAME = '" + 
                            txtad.Text + 
                            "' and YPASS = '" + 
                            txtpass.Text + "' ", 
                          con);

试试看。我在您的查询中添加了两个等于运算符。

于 2013-08-30T18:46:57.243 回答