标题:登录,连接数据库告急!!!!!!!!
只看楼主
wcp126
Rank: 1
等 级:等待验证会员
帖 子:95
专家分:7
注 册:2010-3-30
结帖率:90.91%
已结贴  问题点数:30 回复次数:4 
登录,连接数据库告急!!!!!!!!
在窗体一上 有两个文本框,一个用来放用户名,一个用来放密码。
给个实例,我愿意出30分
搜索更多相关主题的帖子: 数据库 告急 登录 
2010-05-30 10:40
封闭
Rank: 9Rank: 9Rank: 9
来 自:广东省汕头市
等 级:蜘蛛侠
威 望:1
帖 子:501
专家分:1084
注 册:2007-9-14
得分:10 
using System.Data;
private string str = "Data Source=.;Initial Catalog=数据库名;Integrated Security=True";        //数据库连接字符串
private SqlConnection conn = new SqlConnection(str);

string loginName = txtLoginName.Text.Trim();        //得到登录名
string loginPwd =txtLoginPwd.Text.Trim();           //得到密码

string sql = string.Format("select * from user where loginName = {0} and loginPwd = {1}" , loginName , loginPwd);
int result = 0;        //如果大于1,则有数据存在
try
{
    SqlCommand cmd = new SqlCommand(sql, conn);
    conn.Open();
    result = (int)cmd.ExecuteScalar();
}
catch
{
}
finally
{
    conn.close();
}
if(result > 0)
    MessageBox.Show("登录成功!");
else
    MessageBox.Show("用户名或密码错误 !")
2010-05-30 12:52
小呆呆
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2010-5-30
得分:10 
   private void btndenglu_Click(object sender, EventArgs e)
        {
            //FrmMain frm = new FrmMain();
            //this.Hide();
            //frm.Show();

            string st = "select * from user  where Name='" + txtLoginName.Text + "'and Password='" + txtLoginPwd.Text + "'";
            SqlConnection con = new SqlConnection("server=.;database=QYGL;uid=sa;pwd=123456");
            con.Open();
            SqlCommand sd = new SqlCommand(st, con);
            SqlDataReader str = sd.ExecuteReader();
根据这样的格式,换上你的属性名
               if (str.Read())
            {
                FrmMain formMain = new FrmMain();
                this.Hide();
                formMain.Show();
            }
            else
            {
                MessageBox.Show("密码错误!");
            }
            str.Close();
            con.Close();
2010-05-30 18:18
wcp126
Rank: 1
等 级:等待验证会员
帖 子:95
专家分:7
注 册:2010-3-30
得分:0 
con.Open();
这步咋是错的咧
2010-05-31 19:23
鸿硕
Rank: 5Rank: 5
来 自:北京海淀
等 级:职业侠客
帖 子:211
专家分:309
注 册:2009-10-11
得分:10 
看看这个吧!
            string qw = "select * from dengru where zhanghao=@a and mima=@b";
            SqlConnection sqlcon = new SqlConnection("server=.;database=qiyexinxi;uid=sa;pwd=123456");
            if (textBox1.Text == "")
            {
                MessageBox.Show("请您输入密码!");
            }
            else
            {
                try
                {
                    sqlcon.Open();
                    SqlCommand sqlcom = new SqlCommand(qw, sqlcon);
                    SqlParameter[] sqldt = new SqlParameter[]
                {
                    new SqlParameter ("@a",comboBox1 .Text.ToString ()),
                    new SqlParameter ("@b",textBox1 .Text.ToString () )
                };
                    sqlcom.Parameters.AddRange(sqldt);
                    int aa = Convert.ToInt32(sqlcom.ExecuteScalar());
                    if (aa > 0)
                    {
                        timer1.Enabled = false;
                        timer1.Stop();
                        主窗体 fgh = new 主窗体();
                        fgh.Show();
                        this.Visible = false;
                    }
                    else
                    {
                        MessageBox.Show("密码错误!请核对后重新登入···");
                    }

点滴产生差距
时间创造奇迹
2010-05-31 23:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-308691-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.117689 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved