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

用C#做一个用户登录页面,登录按钮验证用户名和密码


服务器名leslie-pc 数据库名db_11表名Table_1表项‘用户名’‘密码’,怎么实现登录按钮的功能,从数据库验证有没有该用户,还有注册按钮,弄了一下午没有弄好,就是数据库的查询和验证,最好给出能运行代码,数据库是SQl server --------------------编程问答-------------------- --------------------编程问答--------------------

public partial class Form1 : Form
    {
        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);

            try {
                string sql = "select * from  Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);

                con.Open();
                //cmd.Connection = con;
                
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0) {
                    MessageBox.Show("成功!");
                } else {
                    MessageBox.Show("失败!");
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            } finally {
                con.Close();
            }
        }
    }


--------------------编程问答-------------------- 发错地方了。。 --------------------编程问答--------------------
引用 2 楼 KumaPower 的回复:

public partial class Form1 : Form
    {
        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);

            try {
                string sql = "select * from  Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);

                con.Open();
                //cmd.Connection = con;
                
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0) {
                    MessageBox.Show("成功!");
                } else {
                    MessageBox.Show("失败!");
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            } finally {
                con.Close();
            }
        }
    }


大神运行不了, --------------------编程问答-------------------- MyDB 这个值是在web.config里面配置的
里面有一个节点 <connectionStrings> <add name="MyDB" ......></add></connectionStrings>
http://wenku.baidu.com/view/ff0f8ac69ec3d5bbfd0a740f.html
你看这个一下吧,应该有帮助。 --------------------编程问答-------------------- 未处理的“System.NullReferenceException”类型的异常出现在 WindowsDenglu.exe 中。

其他信息: 未将对象引用设置到对象的实例 --------------------编程问答--------------------
引用 3 楼 mh_ma 的回复:
发错地方了。。
需要发到哪里 --------------------编程问答--------------------
引用 7 楼 cc198908231511 的回复:
Quote: 引用 3 楼 mh_ma 的回复:

发错地方了。。
需要发到哪里

技术区,非技术区很少人回答你的问题 --------------------编程问答--------------------
引用 8 楼 mh_ma 的回复:
Quote: 引用 7 楼 cc198908231511 的回复:

Quote: 引用 3 楼 mh_ma 的回复:

发错地方了。。
需要发到哪里

技术区,非技术区很少人回答你的问题
我发技术区都给我挪出来了 --------------------编程问答--------------------
引用 4 楼 cc198908231511 的回复:
大神运行不了,


我那个正如5楼所说的,是在App.config文件中设置了数据库连接的字符串:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
                <!--本机SQL,数据库名称:练习-->
<add name="MyDB" connectionString="Data Source=localhost;Initial Catalog=练习;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>



我看了你再c#版块发的贴,你把你原来的代码:

 string sql="select * from  Table_1 where 用户名='"+this.textBox1.Text+"',密码='"+this.textBox2.Text+"'";
    SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
    int i=sqlcmd.ExecuteNonQuery();
    if(i!=0){判断用户名和密码} 

换成:

                string sql = "select * from  Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
                SqlCommand cmd = new SqlCommand(sql, conn);

                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);

                conn.Open();
                
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0) {
                    MessageBox.Show("成功!");
                } else {
                    MessageBox.Show("失败!");
                }

应该就可以了。 --------------------编程问答-------------------- 要么你就把你的所有代码发上来。。。 --------------------编程问答--------------------
引用 11 楼 KumaPower 的回复:
要么你就把你的所有代码发上来。。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsApplication10
{
    public partial class Form1 : Form
    {
        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                string sql = "select * from  Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);

                con.Open();
                //cmd.Connection = con;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("成功!");
                }
                else
                {
                    MessageBox.Show("失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}
--------------------编程问答--------------------
引用 12 楼 cc198908231511 的回复:
Quote: 引用 11 楼 KumaPower 的回复:

要么你就把你的所有代码发上来。。。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsApplication10
{
    public partial class Form1 : Form
    {
        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                string sql = "select * from  Table_1 where Table_1.用户名=@username and Table_1.密码=@pwd";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);

                con.Open();
                //cmd.Connection = con;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("成功!");
                }
                else
                {
                    MessageBox.Show("失败!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}
未处理的“System.NullReferenceException”类型的异常出现在 WindowsApplication10.exe 中。

其他信息: 未将对象引用设置到对象的实例。 --------------------编程问答--------------------
引用 13 楼 cc198908231511 的回复:


。。。。。。。发你原来的代码,不是我那代码。。。
补充:.NET技术 ,  非技术区
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,