新浪微博 登陆  注册   设为首页 加入收藏

问题 2013-02-25 08:18:31     点击:5145

[数据库] 关于c#和sql数据库的链接问题

我写的用户登录的代码,运行时提示user附近有语法错误,请问是错在哪?

namespace WindowsFormsApplication1
{
    public partial class userForm : Form
    {
        public userForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string vname = tBuserNAME.Text;
            string vpass = tBuserPW.Text;

            SqlConnection con = new SqlConnection();
            con.ConnectionString = "server=" + Class1.myserver + ";database=" + Class1.mydb + ";Integrated Security=True";
            con.Open();
            SqlCommand sqlcmd = new SqlCommand("select * from user where userNAME = + vname + and userPW= + vpass+",con); //创建Command对象
            SqlDataReader dr = sqlcmd.ExecuteReader(); //执行后返回DataReader对象
            if (dr.Read())
            {
                Class1.myusername = dr["username"].ToString();
                Class1.myuserpassword = dr["userpassword"].ToString();
            }
            else
            {
                MessageBox.Show("请输入正确的用户信息", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            con.Close();
            this.Close();
        }

        private void userForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}








推荐答案   2013-02-25 08:18:31

你用断点,把sql命令复制粘贴到SQL sever中,看问题,可能是你表名不对











  发表评论
昵称:
(不超过20个字符或10个汉字)

内容: