玖叶教程网

前端编程开发入门

C# 与SQL server2023数据库帐号密码连接(WinForm)方法

登录按钮与方法:

private void button1_Click(object sender, EventArgs e)
        {
            sysAdmin sysAdmin = new sysAdmin()
            {
                 LoginName = this.textBox1.Text,
               
               
                LoninPwd = this.textBox2.Text

            };
            sysAdmin = ckAdmin(sysAdmin);
            
            if (sysAdmin == null)
            {
                MessageBox.Show("账号或农密码错误");
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }

        } 
        private sysAdmin ckAdmin(sysAdmin sysAdmin)
        {
            string sql = "Select LoginId,Role from SysAdmin where LoginName='{0}'and LoginPwd='{1}'";
          

            sql = string.Format(sql, sysAdmin.LoginName, sysAdmin.LoninPwd);
            DataSet dataSet = SQLHelper.GetDataSet(sql);

           
            if (dataSet !=null & dataSet.Tables.Count == 1)
            {
                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    sysAdmin.LoginId =Convert.ToInt32( dataSet.Tables[0].Rows[0]["LoginId"]);
                    sysAdmin.Role = Convert.ToInt32(dataSet.Tables[0].Rows[0]["Role"]);
                    return sysAdmin;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

新建一个类:

private static string connStr = "Server=Mirco-2023WPRPQ;DataBase=TeDB;Uid=sa;Pwd=123456";

        public static int ExcuteNonQuery(string sql)
        {
            SqlConnection sqlConnection = new SqlConnection(connStr);//创建一个连接
            

            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
            try
            {
                sqlConnection.Open();//打开连接
                return sqlCommand.ExecuteNonQuery();//执行 增删改 
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {

                sqlConnection.Close();//关闭
            }
            
        }
        public static DataSet GetDataSet(string sql)
        {
            SqlConnection sqlConnection = new SqlConnection(connStr);//创建一个连接


            SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);//创建一个命令
            SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);
            DataSet dataSet = new DataSet();
            try
            {
                sqlConnection.Open();
                adapter.Fill(dataSet);
                return dataSet;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {

                sqlConnection.Close();//关闭
            }
        }

登录类名称:

public class sysAdmin
    {
        public int LoginId { get; set; }
        public string LoginName { get; set; }
        public string LoninPwd { get; set; }
        public int Role { get; set; }
    }

Main主入口点更改:

 static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            Form2 form = new Form2();
            DialogResult resuit = form.ShowDialog();
            if (resuit == DialogResult.OK)
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Exit();
            }
        }
    }

新建一个SQL server2023数据库:

use master
go
if exists(select * from sysdatabases where name='TeTB')
drop database TeDB
go
create database TeDB
on primary
(
    name='TeDB_data',
    filename='K:\DB\TeDB_data.mdf',
    size=100MB,
    filegrowth=50MB
    
)
--必须先在K盘建一个文件夹DB
log on
(
    name='TeDB_log',
    filename='K:\DB\TeDB_log.ldf',
    size=100MB,
    filegrowth=50MB
)
go

--创建数据表

use TeDB
go
if exists(select * from sysobjects where name='SysAdmin')
drop table SysAdmin
go

create table SysAdmin
(
   LoginId int identity(1000,1) primary key,
   LoginName varchar(100) not null,
   LoginPwd varchar(100) not null,
   Role int not null
 )
 
 --添加一组数据
 insert into SysAdmin(LoginName,LoginPwd,Role) values('Admin','123',1)
 
 --册除数据
 delete from SysAdmin
 --查看数据
 select * from SysAdmin

#头条文章养成计划##头条创作挑战赛##暑期创作的大赛#

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言