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("数据库关闭");
}
}
}
}
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("数据库关闭");
}
}
}
}