C#16.1章节数据库连接问题
发表在C#图书答疑
2014-04-05
是否精华
是
否
版块置顶:
是
否
C#16.1章节问题
书上是这个代码,我要用windows认证登陆,请问怎么改呢?
服务器名是不是下图中的名称?还是ip地址
namespace ConSQL
{
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=taoyu-pc;database=" + textBox1.Text.Trim() ;
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
label2.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
}
}
catch
{
MessageBox.Show("连接数据库失败");
}
}
}
}
}
[img src=http://v2.freep.cn/3tb_140405113200ybds512293.jpg/img]
书上是这个代码,我要用windows认证登陆,请问怎么改呢?
服务器名是不是下图中的名称?还是ip地址
namespace ConSQL
{
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=taoyu-pc;database=" + textBox1.Text.Trim() ;
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
label2.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
}
}
catch
{
MessageBox.Show("连接数据库失败");
}
}
}
}
}
[img src=http://v2.freep.cn/3tb_140405113200ybds512293.jpg/img]