当前位置:编程学习 > C#/ASP.NET >>

新人求解这是什么问题。。。我代码哪里错了。。。

这是APP-CODE里面出错位置的代码
  public Boolean ExecuteQuery(string sql)
    {
        OleDbConnection con = new OleDbConnection(connStr1); 
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Connection = con;
        cmd.CommandText = sql;
        con.Open();
        OleDbDataReader dr = cmd.ExecuteReader();
        if(dr.Read())
        {
            return true;
        }
        else
        {
            return false;
        }
     }
这是网页错误的代码。。。。。是个登陆界面来的。。
protected void Button1_Click(object sender, EventArgs e)
    {
        string userID = TextBox2.Text.Trim();
        string userpsd = TextBox1.Text.Trim();
        Session["userID"] = userID;
        Session["userpsd"] = userpsd;
        db mydr1 = new db();
        if (DropDownList1.SelectedItem.Value == "管理员")
        {
            string sql1 = "select * from admin where 用户名='" + userID + "'and 密码='" + userpsd + "'";
            if (!mydr1.ExecuteQuery(sql1))
            {
                Label3.Text = "您输入的用户名或密码有误!";
            }
            else
            {
                Response.Redirect("Index2.aspx");
            }
        }


        if (DropDownList1.SelectedItem.Value == "用户")
        {
            string sql1 = "select * from user where 用户名='" + userID + "'and 密码='" + userpsd + "'";
            if (!mydr1.ExecuteQuery(sql1))
            {
                Label3.Text = "您输入的用户名或密码有误!";
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }

错误信息如下
用户代码未处理 System.Data.OleDb.OleDbException
  Message="FROM 子句语法错误。"
  Source="Microsoft JET Database Engine"
  ErrorCode=-2147217900
  StackTrace:
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       在 System.Data.OleDb.OleDbCommand.ExecuteReader()
       在 db.ExecuteQuery(String sql) 位置 c:\Users\KimaLiszt\Documents\Visual Studio 2008\WebSites\WebSite2\App_Code\db.cs:行号 43
       在 _Default.Button1_Click(Object sender, EventArgs e) 位置 c:\Users\KimaLiszt\Documents\Visual Studio 2008\WebSites\WebSite2\Login.aspx.cs:行号 44
       在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 


楼主已经不知道自己是死在什么地方了。。。。数据库木有问题,代码木有问题,一运行就oledb。。。。。。。。 --------------------编程问答--------------------
string sql1 = "select * from admin where 用户名='" + userID + "'and 密码='" + userpsd + "'";
改为:
string sql1 = "select * from admin where 用户名='" + userID + "' and 密码='" + userpsd + "' ";


and 的前面应该有一个空格,你已经和单引号连接在一起了 --------------------编程问答--------------------
引用 楼主 u013233198 的回复:
这是APP-CODE里面出错位置的代码
  public Boolean ExecuteQuery(string sql)
    {
        OleDbConnection con = new OleDbConnection(connStr1); 
        OleDbCommand cmd = new OleDbCommand(sql, con);
        cmd.Connection = con;
        cmd.CommandText = sql;
        con.Open();
        OleDbDataReader dr = cmd.ExecuteReader();
        if(dr.Read())
        {
            return true;
        }
        else
        {
            return false;
        }
     }
这是网页错误的代码。。。。。是个登陆界面来的。。
protected void Button1_Click(object sender, EventArgs e)
    {
        string userID = TextBox2.Text.Trim();
        string userpsd = TextBox1.Text.Trim();
        Session["userID"] = userID;
        Session["userpsd"] = userpsd;
        db mydr1 = new db();
        if (DropDownList1.SelectedItem.Value == "管理员")
        {
            string sql1 = "select * from admin where 用户名='" + userID + "'and 密码='" + userpsd + "'";
            if (!mydr1.ExecuteQuery(sql1))
            {
                Label3.Text = "您输入的用户名或密码有误!";
            }
            else
            {
                Response.Redirect("Index2.aspx");
            }
        }


        if (DropDownList1.SelectedItem.Value == "用户")
        {
            string sql1 = "select * from user where 用户名='" + userID + "'and 密码='" + userpsd + "'";
            if (!mydr1.ExecuteQuery(sql1))
            {
                Label3.Text = "您输入的用户名或密码有误!";
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }

错误信息如下
用户代码未处理 System.Data.OleDb.OleDbException
  Message="FROM 子句语法错误。"
  Source="Microsoft JET Database Engine"
  ErrorCode=-2147217900
  StackTrace:
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       在 System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       在 System.Data.OleDb.OleDbCommand.ExecuteReader()
       在 db.ExecuteQuery(String sql) 位置 c:\Users\KimaLiszt\Documents\Visual Studio 2008\WebSites\WebSite2\App_Code\db.cs:行号 43
       在 _Default.Button1_Click(Object sender, EventArgs e) 位置 c:\Users\KimaLiszt\Documents\Visual Studio 2008\WebSites\WebSite2\Login.aspx.cs:行号 44
       在 System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       在 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 


楼主已经不知道自己是死在什么地方了。。。。数据库木有问题,代码木有问题,一运行就oledb。。。。。。。。

+1 --------------------编程问答--------------------
引用 1 楼 hdhai9451 的回复:
string sql1 = "select * from admin where 用户名='" + userID + "'and 密码='" + userpsd + "'";
改为:
string sql1 = "select * from admin where 用户名='" + userID + "' and 密码='" + userpsd + "' ";


and 的前面应该有一个空格,你已经和单引号连接在一起了


我试了试好像还是不行。。。而且现在发现更吊的问题。。。。。只要链接进用户名选项,就是from错误,管理员就不会 --------------------编程问答-------------------- 成功解决了。。。。。。。。

将数据库表名称修改。。。就没事了。。。。。。。。。 --------------------编程问答-------------------- admin是关键词
要改成[admin]
select * from admin
改成
select * from [admin] --------------------编程问答--------------------
引用 5 楼 c02645 的回复:
admin是关键词
要改成[admin]
select * from admin
改成
select * from [admin]

user也要改成[user] --------------------编程问答--------------------
引用 6 楼 c02645 的回复:
Quote: 引用 5 楼 c02645 的回复:

admin是关键词
要改成[admin]
select * from admin
改成
select * from [admin]

user也要改成[user]


谢谢了。。。。我将表名称统统修改了。。。为了避免关键词 --------------------编程问答-------------------- 苦逼的楼主,这种小儿科的问题也拿出来
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,