标题:C#连接SQL2005的登陆系统有问题,求大神指点
只看楼主
xintuhai
Rank: 1
等 级:新手上路
帖 子:42
专家分:0
注 册:2014-10-12
结帖率:33.33%
 问题点数:0 回复次数:2 
C#连接SQL2005的登陆系统有问题,求大神指点
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Reflection;


namespace DP_Capability
{
    public partial class Login : Form
    {
        
        public Login()
        {
            InitializeComponent();
        }

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

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                textBox2.Focus();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                button1.Focus();
        }
      
        private void button1_Click(object sender, EventArgs e)
        {
            //字符串赋值:用户名和密码
            string username = textBox1.Text.Trim();
            string userpwd = textBox2.Text.Trim();

            //定义数据库连接语句
            string consqlserver = "Data Source=.;Initial Catalog=DP Capability;Integrated Security=True;";

            //定义SQL查询语句:用户名和密码
            string sql = "select * from login where uid='" + username + "' and pwd='" + userpwd + "' ";

            //定义SQL Server连接对象 打开数据库
            SqlConnection Connection = new SqlConnection(consqlserver);
            Connection.Open();

            //定义查询命令:表示对数据库执行一个SQL语句或存储过程
            SqlCommand com = new SqlCommand(sql, Connection);

            //执行查询:提供一种读取数据库行的方式   
            SqlDataReader sread = com.ExecuteReader();
            try
            {

                //如果存在用户名和密码正确数据执行进入系统操作  
                if (sread.Read())
                {
                    //MessageBox.Show("登录成功");
                    this.Close();
                    NavalModel NM = new NavalModel();
                    NM.Show();
                }
                else
                {
                    MessageBox.Show("请输入正确的用户名和密码");
                }
            }

            catch (Exception msg)
            {

                throw new Exception(msg.ToString());  //处理异常信息
            }
            finally
            {
                Connection.Close();                    //关闭连接
                Connection.Dispose();                  //释放连接  
                sread.Dispose();                //释放资源
            }
        }
     }
}
搜索更多相关主题的帖子: private public 
2015-03-15 12:47
XQY521
Rank: 2
来 自:神秘之国
等 级:论坛游民
帖 子:15
专家分:27
注 册:2015-3-30
得分:0 
我们用的数据库不一样
2015-03-31 13:54
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:0 
是代码出问题了还是数据库链接不上了? 如果是2005首先链接字符串呃实例就不对

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2015-03-31 14:31



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




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

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