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

cc198908231511 2013-05-22 03:43:11

服务器名leslie-pc 数据库名db_11表名Table_1表项‘用户名’‘密码’,怎么实现登录按钮的功能,从数据库验证有没有该用户,还有注册按钮,弄了一下午没有弄好,就是数据库的查询和验证,最好给出能运行代码,数据库是SQl server
...全文
28967 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Bonjour-你好 2013-05-23
  • 打赏
  • 举报
回复
引用 13 楼 cc198908231511 的回复:
。。。。。。。发原来的代码,不是我那代码。。。
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
引用 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();
            }
        }
    }
}
[/quote]未处理的“System.NullReferenceException”类型的异常出现在 WindowsApplication10.exe 中。 其他信息: 未将对象引用设置到对象的实例。
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
引用 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();
            }
        }
    }
}
Bonjour-你好 2013-05-23
  • 打赏
  • 举报
回复
要么你就把你的所有代码发上来。。。
Bonjour-你好 2013-05-23
  • 打赏
  • 举报
回复
引用 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("失败!");
                }
应该就可以了。
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
引用 8 楼 mh_ma 的回复:
引用 7 楼 cc198908231511 的回复:
[quote=引用 3 楼 mh_ma 的回复:] 发错地方了。。
需要发到哪里
技术区,非技术区很少人回答你的问题[/quote]我发技术区都给我挪出来了
王子文龙 2013-05-23
  • 打赏
  • 举报
回复
引用 7 楼 cc198908231511 的回复:
引用 3 楼 mh_ma 的回复:
发错地方了。。
需要发到哪里
技术区,非技术区很少人回答你的问题
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
引用 3 楼 mh_ma 的回复:
发错地方了。。
需要发到哪里
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
未处理的“System.NullReferenceException”类型的异常出现在 WindowsDenglu.exe 中。 其他信息: 未将对象引用设置到对象的实例
qq335349725 2013-05-23
  • 打赏
  • 举报
回复
MyDB 这个值是在web.config里面配置的 里面有一个节点 <connectionStrings> <add name="MyDB" ......></add></connectionStrings> http://wenku.baidu.com/view/ff0f8ac69ec3d5bbfd0a740f.html 你看这个一下吧,应该有帮助。
cc198908231511 2013-05-23
  • 打赏
  • 举报
回复
引用 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();
}
}
}


大神运行不了,
王子文龙 2013-05-22
  • 打赏
  • 举报
回复
发错地方了。。
Bonjour-你好 2013-05-22
  • 打赏
  • 举报
回复

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();
}
}
}


cc198908231511 2013-05-22
  • 打赏
  • 举报
回复

7,774

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧