SQL Server的基本操作

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;


//using DataBase;


namespace 数据库Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            创建操作数据库的到单例对象
            //DBOption.Instance.CreateDB();
            //MessageBox.Show(DBOption.Instance.ToString());




            //对控件进行初始化设置
            radioButton1.Checked = true;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            //对密码框进行密码设置
            textBox4.PasswordChar = '*';
        }


        private void label1_Click(object sender, EventArgs e)
        {


        }


        private void label2_Click(object sender, EventArgs e)
        {


        }


        //当选中第二个单选框时,用户名和密码框需要输入
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            //方法一
            //if (radioButton2.Checked)
            //{
            //    textBox3.Enabled = true;
            //    textBox4.Enabled = true;
            //}
            //else {
            //    textBox3.Clear();
            //    textBox4.Clear();
            //    textBox3.Enabled = false;
            //    textBox4.Enabled = false;
            //}
            
            //方法二:根据第二个单选按钮的可用性更新用户名和密码框的可用性
            textBox3.Clear();
            textBox4.Clear();
            textBox3.Enabled = radioButton2.Checked;
            textBox4.Enabled = radioButton2.Checked;


        }


        //根据连接字符串进行相应的连接
        private void button1_Click(object sender, EventArgs e)
        {
            //创建一个连接字符串
            String ConnectionStr = null;
            //服务器名
            ConnectionStr = "Data Source="+textBox1.Text;
            //数据库名
            ConnectionStr += ";Initial Catalog="+textBox2.Text;
            //按照windows进行连接
            if (radioButton1.Checked)
            {
                ConnectionStr += ";Integrated Security=true";
            }
                //按照Sql Server进行连接
            else {
                //用户名
                ConnectionStr += ";user id=" + textBox3.Text;
                ConnectionStr += ";password=" + textBox4.Text;
            }
            //创建连接对象
            SqlConnection con = new SqlConnection(ConnectionStr);
            try
            {
                con.Open();
                MessageBox.Show("数据库连接成功!");


            }
            catch (Exception ex)
            {
                MessageBox.Show("数据库连接失败,异常信息是:" + ex.Message);
            }
            finally {
                con.Close();
                con.Dispose();
                MessageBox.Show("数据库关闭");
             }


        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值