C#链接数据库:SQL Server 2008
时间:2021-07-01 10:21:17
帮助过:2人阅读
windows验证方式
string connectionStringTest = @"Data Source
=ADMINISTRATOR\SQLEXPRESS;Initial Catalog
=TestDB;Integrated Security
=SSPI; ";
//建立信任连接(具体含义与同其他方式的区别还需学习)
string connectionStringTest = @"server
=ADMINISTRATOR\SQLEXPRESS;Initial Catalog
=TestDB;Integrated Security
=True";
//网站连接数据库的标准方式
string connectionStringTest = @"server
=ADMINISTRATOR\SQLEXPRESS;
database=TestDB;
user id
=testuser;password
=123456";
//应用程序连接数据库的标准方式
string connectionStringTest = @"Data Source
= ADMINISTRATOR\SQLEXPRESS; Initial Catalog
= TestDB;
User Id
= testuser; Password
= 123456;";
链接、断开、释放资源的代码:
SqlConnection conn = new SqlConnection(connectionStringTest);
try
{
conn.Open();
MessageBox.Show("数据库链接成功!","提示");
}
catch (Exception e)
{
string message = e.Message;
}
finally
{
conn.Close();
conn.Dispose();
}
C#链接数据库:SQL Server 2008
标签: