日期:2014-05-18  浏览次数:20747 次

System.Data.SqlClient.SqlException: 将数据类型 varchar 转换为 numeric 时出错。
C# code
public class DataBase
{
    public DataBase()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    
    //string strCon = "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestOnline";
    private SqlConnection SqlCon = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=TestOnline");
    private SqlCommand SqlCmd;
   // private SqlDataAdapter da;
   // private DataSet ds;

    public int GetUser(string ComStr)
    {
        SqlCon.Open();
        SqlCmd = new SqlCommand(ComStr, SqlCon);
        int i;
        i = Convert.ToInt32(SqlCmd.ExecuteScalar());
        return i;
    }
}



C# code
 protected void Button1_Click(object sender, EventArgs e)
    {
        DataBase db = new DataBase();

        if (this.TextBox1.Text.Trim() == "" || this.TextBox2.Text.Trim() == "" || this.DropDownList1.SelectedValue == "")
        {
            Response.Write("<script>alert(\"账号,密码,用户类型不能为空\");</script>");
        }
        else
        {
            switch (DropDownList1.SelectedValue)
            {
                case "学生":
                    string SelCmd = "SELECT COUNT(*) FROM Student WHERE Name=" + this.TextBox1.Text + "and Pwd='" + this.TextBox2.Text + "'";
                   // int tmp = db.GetUser(SelCmd);
                  //  Response.Write(tmp.GetType());
                    if (db.GetUser(SelCmd) != 0)     [color=#FF0000] //System.Data.SqlClient.SqlException: 将数据类型 varchar 转换为 numeric 时出错。[/color]
                    {
                        Session["StuNo"] = this.TextBox1.Text;
                        Response.Write("<script>alert(\"登陆成功\");</script>");
                    }
                    else
                    {
                        Response.Write("<script>alert(\"账号或密码错误\");<script>");
                    }
                    break;
                default:
                  //  Session["StuNo"] = this.TextBox1.Text;
                 //   Response.Write(Session.ToString());
                    break;
            }
        }
    }


------解决方案--------------------
string SelCmd = "SELECT COUNT(*) FROM Student WHERE Name=" + this.TextBox1.Text + "and Pwd='" + this.TextBox2.Text + "'";
------------------------------------------------
string SelCmd = "SELECT COUNT(*) FROM Student WHERE Name='" + this.TextBox1.Text + "' and Pwd='" + this.TextBox2.Text + "'";


------解决方案--------------------
单引号

"SELECT COUNT(*) FROM Student WHERE Name='" + this.TextBox1.Text + "' and Pwd='" + this.TextBox2.Text + "'"
  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html