• C#链接数据库:SQL Server 2008


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

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

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

    链接数据库的的代码:

    // windows验证方式
    string connectionStringTest = @"Data Source=ADMINISTRATORSQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI; ";
    
    //建立信任连接(具体含义与同其他方式的区别还需学习)
    string connectionStringTest = @"server=ADMINISTRATORSQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True";
    
    //网站连接数据库的标准方式
    string connectionStringTest = @"server=ADMINISTRATORSQLEXPRESS;database=TestDB;user id=testuser;password=123456";
    
    //应用程序连接数据库的标准方式
    string connectionStringTest = @"Data Source = ADMINISTRATORSQLEXPRESS; 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(); }
  • 相关阅读:
    要成功先发疯
    情绪ABC理论
    树立和提高威信法
    javaagent
    sonar 使用
    sonar 代码质量管理
    四大思维工具,SWOT、PDCA、DISC、时间管理
    HyperLogLog
    位数组
    git checkout .和git checkout -f的区别;git add . git add -u git add -A的区别
  • 原文地址:https://www.cnblogs.com/AmatVictorialCuram/p/4732272.html
Copyright © 2020-2023  润新知