欢迎DaLao指导

博客名称

你的一句话介绍

C#链接数据库:SQL Server 2008

 

 

自己学习C#编程,在WinForm编程中,代码测试连接数据库。

现在sqlserver中测试使用的数据库能否以指定的用户名和密码登录。

如图所示,计算机名为administrator,数据库实例为sqlexpress,登录名为testuser,密码设置为123456,用户名映射数据库为TestDB。

链接数据库的的代码:

// 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(); }

 

posted @ 2015-08-15 12:46  麻辣咸鱼  阅读(5421)  评论(0编辑  收藏  举报