从master的sysdatabases表中读。use master
select name from sysdatabases
select name from sysdatabases
解决方案 »
- C#中用显示数据源添加的DataGridView怎样进行更新和删除数据?
- iis 权限配置问题!请教
- 关于Sql Server 数据库删除数据问题。求高手解答~~~
- 在C# 中通过 P/Invoke 调用Win32 DLL 过程中 对结构的定义
- DataGrid控件问题
- 想做一个设计器一样的东西,可以把控件拖到界面上,只要提供思路就给分!
- 一个关于FileStream的菜菜问题!
- 求教winform程序自动升级的方案
- 小问题,RegularExpressionValidator 控件中如何写 Expression 表达式
- 如何回到Form1?
- C#还能使用BHO吗?如果能100分求C#使用BHO(browser helper object)的例子。
- 在菜单中打开MDI窗口,但是每一次点击,就出现一个新窗口,如何只出现一个?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;namespace SQL_Tools
{
/// <summary>
/// connection 的摘要说明。
/// </summary>
public class connection : Form
{
private RadioButton radioButton1;
private Label label1;
private RadioButton radioButton2;
private Label label2;
private Label label3;
private TextBox textBox1;
private TextBox textBox2;
private GroupBox groupBox1;
private GroupBox groupBox2;
private Button butOK;
private Button butCancel;
private string database;
internal string ConnText="";
private GroupBox groupBox3;
private Label label4;
private ComboBox comboBox2;
private System.Windows.Forms.TextBox textBox3;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private Container components = null; public connection()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.label1 = new System.Windows.Forms.Label();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.butOK = new System.Windows.Forms.Button();
this.butCancel = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.label4 = new System.Windows.Forms.Label();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// radioButton1
//
this.radioButton1.Location = new System.Drawing.Point(24, 112);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(152, 24);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "Windows身份验证(&W)";
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 2;
this.label1.Text = "服务器:";
//
// radioButton2
//
this.radioButton2.Location = new System.Drawing.Point(24, 136);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(160, 24);
this.radioButton2.TabIndex = 0;
this.radioButton2.Text = "SQL Server身份验证(&Q)";
this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(56, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 23);
this.label2.TabIndex = 2;
this.label2.Text = "登录名:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(56, 200);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(56, 23);
this.label3.TabIndex = 2;
this.label3.Text = "密码:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(120, 168);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(120, 200);
this.textBox2.Name = "textBox2";
this.textBox2.PasswordChar = '*';
this.textBox2.Size = new System.Drawing.Size(168, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(16, 48);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 8);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(16, 224);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(280, 8);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
//
// butOK
//
this.butOK.Location = new System.Drawing.Point(64, 240);
this.butOK.Name = "butOK";
this.butOK.TabIndex = 5;
this.butOK.Text = "连接(&L)";
this.butOK.Click += new System.EventHandler(this.butOK_Click);
//
// butCancel
//
this.butCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.butCancel.Location = new System.Drawing.Point(184, 240);
this.butCancel.Name = "butCancel";
this.butCancel.TabIndex = 5;
this.butCancel.Text = "取消(&C)";
this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
//
// groupBox3
//
this.groupBox3.Location = new System.Drawing.Point(16, 96);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(280, 8);
this.groupBox3.TabIndex = 4;
this.groupBox3.TabStop = false;
//
// label4
//
this.label4.Location = new System.Drawing.Point(40, 64);
this.label4.Name = "label4";
this.label4.TabIndex = 2;
this.label4.Text = "数据库:";
//
// comboBox2
//
this.comboBox2.Items.AddRange(new object[] {
"master",
"model",
"msdb",
"Northwind",
"pubs",
"tempdb"});
this.comboBox2.Location = new System.Drawing.Point(96, 64);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(176, 20);
this.comboBox2.TabIndex = 1;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(96, 16);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(176, 21);
this.textBox3.TabIndex = 6;
this.textBox3.Text = "(local)";
this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
//
// connection
//
this.AcceptButton = this.butOK;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.CancelButton = this.butCancel;
this.ClientSize = new System.Drawing.Size(306, 277);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox3,
this.comboBox2,
this.butOK,
this.groupBox1,
this.textBox1,
this.label1,
this.radioButton1,
this.radioButton2,
this.label2,
this.label3,
this.textBox2,
this.groupBox2,
this.butCancel,
this.groupBox3,
this.label4});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "connection";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "连接数据库";
this.Load += new System.EventHandler(this.connection_Load);
this.ResumeLayout(false); }
#endregion private void butCancel_Click(object sender, System.EventArgs e)
{
Application.Exit();
} private void connection_Load(object sender, System.EventArgs e)
{
string ConnText="database=master;integrated security=sspi;server="+this.textBox3.Text.Trim().ToString()+";";
string CommandText="SELECT dbid,[name] FROM SYSDATABASES";
SqlConnection Conn=new SqlConnection();
Conn.ConnectionString=ConnText;
SqlCommand Select=new SqlCommand();
Select.CommandText=CommandText;
Select.Connection=Conn;
SqlDataAdapter SqlDa=new SqlDataAdapter(Select);
DataSet Ds=new DataSet();
try
{
Conn.Open();
}
catch(SqlException sqlex)
{
MessageBox.Show(sqlex.Message);
}
finally
{
Conn.Close();
}
try
{
Conn.Open();
SqlDa.Fill(Ds,"sysdatabases");
this.comboBox2.DataSource=Ds.Tables["sysdatabases"];
this.comboBox2.DisplayMember="name";
this.comboBox2.ValueMember="dbid";
}
catch(SqlException sqlex)
{
MessageBox.Show(sqlex.Message,"出错");
}
finally
{
Conn.Close();
}
radioButton1.Checked=true;
if(radioButton2.Checked==true)
{
textBox1.Enabled=true;
textBox2.Enabled=true;
}
else
{
textBox1.Enabled=false;
textBox2.Enabled=false;
}
} private void butOK_Click(object sender, System.EventArgs e)
{
Form1 f=new Form1();
if(comboBox2.Text.Trim().Length==0)
{
if(MessageBox.Show("请你选择一个你想登录的数据库!!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning)==DialogResult.OK)
{
comboBox2.Focus();
}
}
else
{
database="database="+comboBox2.Text+";";
}
if(radioButton1.Checked)
{
ConnText="integrated security=sspi;"+database+";server="+this.textBox3.Text+";";
f.label1.Text=ConnText;
}
else
{
if(radioButton2.Checked)
{
if(textBox1.Text.Trim().ToString().Length==0)
{
if(MessageBox.Show("请你输入用户名,默认情况用户名为\"sa\"。","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)==DialogResult.OK)
{
textBox1.Focus();
}
}
}
ConnText="data source="+this.textBox3.Text+";"+"persist security info=False;user id="+textBox1.Text+";"+"password="+textBox2.Text+";"+database+";";
f.label1.Text=ConnText;
}
SqlConnection Conn=new SqlConnection();
Conn.ConnectionString=ConnText;
try
{
Conn.Open();
}
catch(SqlException sqlex)
{
if(MessageBox.Show("数据库连接失败!\r\n失败原因为:"+sqlex.Message,"失败",MessageBoxButtons.OK,MessageBoxIcon.Error)==DialogResult.OK)
{
this.textBox3.Focus();
}
}
finally
{
Conn.Close();
}
if(MessageBox.Show("数据库连接成功!!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information)==DialogResult.OK)
{
this.Hide();
}
} private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
{
textBox1.Enabled=false;
textBox2.Enabled=false;
} private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
{
textBox1.Enabled=true;
textBox2.Enabled=true;
} private void textBox3_TextChanged(object sender, System.EventArgs e)
{
this.connection_Load(sender,e);
}
}
}