SQLserver数据库连接问题(wfa程序登录源代码)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RLHT_YXJ_WFA1
{
    public partial class LogIn : Form
    {
        //string[] table=new string[20];
        public LogIn()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(@"Data Source=YXJ\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Ceshi.mdf;Integrated Security=True;User Instance=true"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText="select user_name,user_pwd from Users Where user_name='"+this.textBox1.Text+"'";
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (this.textBox1.Text == "")

                            MessageBox.Show("请输入您的用户名");
                        else if (this.textBox2.Text == "")

                            MessageBox.Show("请输入您的密码");
                        else if (reader.Read())
                        {
                            //用户名存在
                            string dbpassword = reader.GetString(reader.GetOrdinal("user_pwd"));
                            if (this.textBox2.Text == dbpassword)
                            {
                                //登陆成功
                                ShowUser showuser = new ShowUser(textBox1.Text);
                                showuser.Show();
                            }
                            else
                            {
                                //密码错误
                                MessageBox.Show("您所输入的密码错误");
                                this.textBox2.Text = null;
                            }
                        }
                        else
                        {
                            //用户名错误
                            MessageBox.Show("您所输入的用户名不存在");
                            this.textBox1.Text = null;
                        }
                    }
                }
                conn.Close();
                conn.Dispose();
            }
      
            //数据访问出现问题,明明修改了数据库,但还是可以用修改前的老用户数据进行访问,是缓存问题?有知道的高手烦请解答。
            //DataTable table = new DataTable();
            //SqlDataAdapter da = new SqlDataAdapter();

            //string connstr = @"Data Source=YXJ\SQLEXPRESS;AttachDBFilename=|DataDirectory|\Ceshi.mdf;Integrated Security=True;User Instance=true";
            //SqlConnection connection = new SqlConnection(connstr);
            //connection.Open();
            //SqlCommand ca = new SqlCommand("select user_name,user_pwd from Users Where user_name='"+this.textBox1.Text+"'",connection);
            //da.SelectCommand = ca;
            //da.Fill(table);
            //if (this.textBox1.Text == "")
              
            //    MessageBox.Show("请输入您的用户名");
            //else if (this.textBox2.Text == "")

            //    MessageBox.Show("请输入您的密码");
            //else if (table.Rows.Count == 0)
            //{
            //    MessageBox.Show("您所输入的用户名不存在");
            //    this.textBox1.Text = null;
            //}
            //else if (table.Rows[0][0].ToString() != this.textBox2.Text)
            //{
               
            //    MessageBox.Show("您所输入的密码错误");
            //    this.textBox2.Text = null;
            //}
            //else
            //{
            //    System.Console.WriteLine(table.Rows[0][0].ToString());
            //    System.Console.WriteLine(this.textBox2.Text);
            //    MessageBox.Show(table.Rows[0][0].ToString());
            //    MessageBox.Show(this.textBox2.Text);

            //    ShowUser showuser = new ShowUser(textBox1.Text);
            //    showuser.Show();
            //}
            //connection.Close();
            //table.Clear();
          
        }

        private void button3_Click(object sender, EventArgs e)
        {
            NewUser newuser=new NewUser();
            newuser.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
        }

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

参考:

SqlDataAdapter

http://baike.baidu.com/view/2978109.htm

DataTable

http://blog.csdn.net/hcw_peter/article/details/3980723

关于|DataDirectory|\:

Program.cs

 static void Main()
        {

            string dataDir = AppDomain.CurrentDomain.BaseDirectory;
            if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))
            {
                dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
                AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new LogIn());
        }

//这是为了在测试运行的时候,让项目里的数据库文件和程序Debug或Release运行里的数据库文件区别开,真正交付给客户的时候一般不需要这段代码。

原文地址:https://www.cnblogs.com/wuyida/p/6301188.html