首页 > 代码库 > C#链接SQL Server数据库

C#链接SQL Server数据库

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. namespace SQLHelper {
  8. class Program {
  9. static void Main(string[] args) {
  10. string con;
  11. //Window身份验证
  12. //con = "Server=DESKTOP-SIKEISM\\SQLEXPRESS;Database=MusicStore;Trusted_Connection=SSPI";
  13. //SQL Server身份验证
  14. con = "Server=DESKTOP-SIKEISM\\SQLEXPRESS;Database=MusicStore;uid=sa;pwd=123456";
  15. SqlConnection mycon = new SqlConnection(con);
  16. try {
  17. mycon.Open();
  18. Console.WriteLine("Success!");
  19. } catch (SqlException ex) {
  20. Console.WriteLine("Fail!" + ex.Message);
  21. }
  22. }
  23. }
  24. }




null


C#链接SQL Server数据库