请问如何在c#中连接sql server 2005
我是一名新手,刚学习c#,在书在看到了一个例子,但运行不了,不知为何,请高手指教。下面是程序代码:using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入要连接的数据库名称");
}
else
{
try
{
string ConStr = "server=;database=" + textBox1.Text.Trim() + ";uid=;pwd=;";
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
label2.Text = "数据库[" + textBox1.Text.Trim() + "]已经连接并打开";
}
catch
{
MessageBox.Show("连接数据库失败");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}