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

C#连接SQL


  SqlConnection con = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=playerinfo");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from player_info where name='"+textBox1.Text+"and pwd="+textBox2.Text+ "'", con);
            int state = cmd.ExecuteNonQuery();
            con.Close();
            if (state == 0)
            {
                MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
            }
            else
            {
                Form f = new Introduce();
                this.Hide();
                f.Show();
            }


不知道为什么不行  输入错误的时候还是可以登陆呢  --------------------编程问答-------------------- --------------------编程问答-------------------- "select * from player_info where name='"+textBox1.Text+"and pwd="+textBox2.Text+ "'"
检查下这个sql  好像有问题 --------------------编程问答--------------------
引用 2 楼  的回复:
"select * from player_info where name='"+textBox1.Text+"and pwd="+textBox2.Text+ "'"
检查下这个sql  好像有问题
?????没问题啊  --------------------编程问答-------------------- 来个高手啊 , --------------------编程问答-------------------- "select * from player_info where name="+textBox1.Text+"and pwd="+textBox2.Text+ ""引用c#变量直接可以用双引号 --------------------编程问答-------------------- LS你看清楚了吗 --------------------编程问答-------------------- 如果Sql语句没问题的话,con.Close()有问题。把con.Close()写在if语句外。 --------------------编程问答-------------------- if (state == 0)
            {
                MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
            }
            else
            {
                Form f = new Introduce();
                this.Hide();
                f.Show();
            }
            con.Close();



--------------------编程问答-------------------- select语句不适合用ExecuteNonQuery()方法,你那个state的值永远也是"-1",你怎么操作也没用的,用其他方法吧 --------------------编程问答-------------------- 再补充一下 ExecuteNonQuery()方法只对 UPDATE、INSERT 和 DELETE 语句,才有返回值为该命令所影响的行数。对于其他所有类型的语句,返回值为 -1。 --------------------编程问答-------------------- SqlConnection con = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=playerinfo");//首先检查连接字符串对不对
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from player_info where name='"+textBox1.Text+"' and pwd='"+textBox2.Text+ "'", con);//是字符串要加单引号,该有空格的用空格(and前有空格)
            int state = cmd.ExecuteNonQuery();//既然是select,不用ExecuteNonQuery,用ExecuteScalar()或者sql语句改成SELECT COUNT(*) FROM player_info  where name='"+textBox1.Text+"' and pwd='"+textBox2.Text+ "'
         //  参考http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executescalar.aspx
            con.Close();
            if (state == 0)
            {
                MessageBox.Show("用户不存在,请检测用户名和密码是否正确!");
            }
            else
            {
                Form f = new Introduce();
                this.Hide();
                f.Show();
            } --------------------编程问答-------------------- sql语句中and之前应该有个空格 --------------------编程问答--------------------
引用 11 楼  的回复:
SqlConnection con = new SqlConnection("server=.;Trusted_Connection=yes;user=sa;pwd=1;database=playerinfo");//首先检查连接字符串对不对
            con.Open();
            SqlCommand cmd = new SqlCommand("select……


顶! 这位大侠非常正确! --------------------编程问答-------------------- 楼主还是不要用ExecuteNonQuery来执行查询了吧?建议使用DataAdapter或者DataReader来实现
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,