数据库、服务器设置

本文介绍了一个使用 C# 实现的 SQL 数据库连接配置界面。该界面允许用户选择服务名称、数据库名称,并进行连接测试。此外,还提供了用户登录验证的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private ConnClass.SqlConn sql;
         private ConnClass.SqlConn sql2 = new ConnClass.SqlConn();
         private string connectionString;
         private bool bl = false;
         public static bool visable = false;
         
         private void Form1_Load(object sender, EventArgs e)
         {
             if (!File.Exists("connection.txt"))
             {
                 connectionString = "data source=.;initial catalog=card;integrated security=sspi";
                 sql = new ConnClass.SqlConn(connectionString);
                 writeConnectionString(connectionString);
             }
             else
             {
                 sql = new ConnClass.SqlConn(sql2.readerConnectionString());
             }
             this.cbx_serviecName.Items.Add(".");
             this.cbx_serviecName.Items.Add("(local)");
             this.cbx_serviecName.Items.Add(Environment.MachineName.ToString());
             this.cbx_serviecName.SelectedIndex = 0;
             // System.Net.Dns.GetHostName().ToString();//System.Environment.MachineName.ToString();获取计算机名
             Height = 235;           
             getUerName();
             getDataBase();
         }
         
         private void writeConnectionString(string connStr)
         {
            FileStream fs = new FileStream("connection.txt", FileMode.Create);
             StreamWriter sw = new StreamWriter(fs);
             try
             {
                 sw.Write(connectionString);
             }
             catch { }
             finally
             {
                 sw.Close();
             }
         }
         private void getDataBase()
         {
             string m_sDBName = "select name from master..sysdatabases";
             if (sql.dataSet(m_sDBName, "master..sysdatabases") != null)
             {
                 int n = sql.dataSet(m_sDBName, "master..sysdatabases").Tables[0].Rows.Count;
                 for (int i = 0; i < n; i++)
                 {
                     this.cbx_DBName.Items.Add(sql.dataSet(m_sDBName, "master..sysdatabases").Tables[0].Rows[i][0].ToString());
                 }
             }
             this.cbx_DBName.SelectedIndex = 0;
         }



//########登录的存储过程调用###############
private void btn_login_Click(object sender, EventArgs e)
         {
             string name = this.cbx_uerName.SelectedItem.ToString();
             string password = this.txt_passWord.Text.Trim();
             SqlParameter[] parameter = new SqlParameter[2];
             parameter[0] = new SqlParameter("@name", SqlDbType.VarChar, 10);
             parameter[0].Value = name;
             parameter[1] = new SqlParameter("@passwd", SqlDbType.VarChar, 20);
             parameter[1].Direction = ParameterDirection.Output;
             sql.isExistProcudure("proLogin", parameter);
             if (parameter[1].Value.Equals(password))
             {
                 visable = true;
                 this.Close();
             }
             else
             {
                 MessageBox.Show("密码错误!!!", "系统登录失败");
                 this.txt_passWord.Text = "";
                 this.txt_passWord.Focus();
             }           
         }         


private void btn_testConn_Click(object sender, EventArgs e)
         {
             string servieceName= this.cbx_serviecName.SelectedItem.ToString();
             string databaseName = this.cbx_DBName.SelectedItem.ToString();
             string   dbUerName= this.txt_DBUserName.Text.Trim();
             string dbPwd = this.txt_DBPwd.Text.Trim();
             
             try
             {
                 SqlConnection conn = new SqlConnection(connectionString);
                 conn.Open();
                 if (this.cbx_DBName.SelectedItem.ToString() == "card")
                 {
                     connectionString = "Data Source=" + servieceName + ";Initial Catalog=" + databaseName + ";Integrated Security=True;uid=" + dbUerName + ";pwd=" + dbPwd + ";";
                     if (conn.State == ConnectionState.Open)
                     {
                         MessageBox.Show("连接成功!!!", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
                         if (this.cbx_windows.Checked)
                         {
                             this.writeConnectionString("data source=.;initial catalog=bearmanage;integrated security=sspi");   
                         }
                         if (this.cbx_SQLServer.Checked)
                         {
                             this.writeConnectionString(connectionString);
                         }
                         conn.Close();
                         this.txt_DBUserName.Text = "";
                         this.txt_DBPwd.Text = "";
                         this.cbx_SQLServer.Checked = false;
                         this.cbx_serviecName.Enabled = false;
                         this.txt_DBUserName.Enabled = false;
                         this.txt_DBPwd.Enabled = false;
                     }
                 }
                 else
                 {
                     MessageBox.Show("数据库错误!!!", "错误信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 }
             }
             catch
             {
                 MessageBox.Show("测试连接失败,请重新输入!!!", "信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                 this.cbx_uerName.Items.Clear();
                 this.txt_DBUserName.Text = "";
                 this.txt_DBPwd.Text = "";
             }
             finally
             {                 
                 
             }
         }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值