111,098
社区成员




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 FormTabNew
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string strCon = "";
public static string sqlServer = "";//用于Form3的数据库表名传递
private DataTable getTable(string str)//获取数据库清单
{
try
{
SqlConnection sqlcon = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases ", sqlcon);
DataTable dt = new DataTable("sysdatabases");
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
}
private void button1_Click(object sender, EventArgs e) //打开选择服务器对话框
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
comboBox2.Text = Form2.strServer;//获取Form2中选中的服务器名称
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)//点击单选项“Windows身份登录”
{
textBox2.Enabled = textBox3.Enabled = false;
string str = "server=" + comboBox2.Text + ";database=master;Integrated Security=SSPI;";
comboBox1.DataSource = getTable(str);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "name";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)//点击单选项“SQL Server身份验证”
{
textBox2.Enabled = textBox3.Enabled = true;
textBox2.Focus();
}
private void button3_Click(object sender, EventArgs e)//点击按钮“登录”
{
if (radioButton1.Checked == true)//方案“Windows身份登录”
{
strCon = "Data Source=" + comboBox2.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;";
}
else if (radioButton2.Checked == true)//方案“SQL Server身份验证”
{
strCon = "Data Source=" + comboBox2.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox2.Text + ";Pwd=" + textBox3.Text + ";";
}
SqlConnection sqlcon = new SqlConnection(strCon);//更新参数strCon
try
{
sqlcon.Open();//数据库可以连接
sqlServer = comboBox1.Text;//给sqlServer赋值,使之等于comboBox1.Text
this.Hide();//当前窗体隐藏
Form3 frm3 = new Form3();
frm3.Show();//显示窗体Form3
}
catch
{
MessageBox.Show("用户或密码错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void button4_Click(object sender, EventArgs e)//点击按钮“取消”
{
Application.Exit();//全部退出结束窗体
}
}
}
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;
namespace FormTabNew
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
textBox1.Text = Form1.sqlServer;//获取Form中的数据库名
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)//触发窗体关闭事件
{
}
}
//如何实现对当前获取的数据库名称进行增删改查操作?
}
public partial class BaseForm : Form
{
public static string Con { get; set; }
public BaseForm()
{
}
}
创建一个窗体基类,公开一个静态属性,其他窗体继承这个基类。