当前位置:Gxlcms > mysql > C#连接SQLServer的常用字符串

C#连接SQLServer的常用字符串

时间:2021-07-01 10:21:17 帮助过:192人阅读

一:C#连接SQL数据库 DataSource=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword; DataSource=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=myDataBase;User ID=myUsername;Password=myPassword; Ser

  一:C#连接SQL数据库

  DataSource=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Password=myPassword;

  DataSource=190.190.200.100,1433;Network Library=DBMSSOCN;InitialCatalog=myDataBase;User ID=myUsername;Password=myPassword;

  Server=myServerAddress;Database=myDataBase;UserID=myUsername;Password=myPassword;

Trusted_Connection=False;

  Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

  Server=myServerNametheInstanceName;Database=myDataBase;Trusted_Connection=True;

  DataSource=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

  1:Integrated Security参数

  当设置Integrated Security为 True 的时候,连接语句前面的UserID, PW 是不起作用的,即采用windows身份验证模式。

  只有设置为 False 或省略该项的时候,才按照 UserID, PW 来连接。

  IntegratedSecurity 还可以设置为:sspi,相当于True,建议用这个代替True.

  DataSource=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

  DataSource=myServerAddress;Initial Catalog=myDataBase;Integrated Security=true;

  DataSource=myServerAddress;Initial Catalog=myDataBase;;UserID=myUsername;

Password=myPasswordIntegrated Security=false;

  2:参数Trusted_Connection

  Trusted_Connection=true,将使用当前的 Windows 帐户凭据进行身份验证

  Trusted_Connection=false;将不采用信任连接方式(也即不采用Windows验证方式),而改由SQL Server 2000验证方式

  Server=myServerAddress;Database=myDataBase;UserID=myUsername;

Password=myPassword;Trusted_Connection=false;

  Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

  3:Initial Catalog是你要连接的数据库的名字

  4:WINCE连接

  DataSource=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;UserID=myDomainmyUsername;Password=myPassword;

  二:可以利用SqlConnectionStringBuilder,这样不必去记住名称。

  SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();

  scsb.DataSource =@"(local)SQLExpress";

  scsb.IntegratedSecurity = true;

  scsb.InitialCatalog = "Northwind";

  SqlConnectionmyConnection = new SqlConnection(scsb.ConnectionString);

  三:可以利用属性中的Setting来自动设置连接字符串

  1:在type中选择(connection string),

  2:在DataSouce中选择数据源,,然后再Server中输入服务器名,本地用(local)SQLExpress

  3:选择登陆验证方式,本次选Windows验证(即信任连接IntegratedSecurity=True)

  4:选择数据库名,确认即可

  DataSource=(local)SQLExpress;Initial Catalog=Northwind;Integrated Security=True

  server =.sqlexpress;integrated security = true;database = northwind

  最后附上一个我自己的测试用例:

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Text;

  using System.Threading.Tasks;

  using System.Data;

  using System.Data.SqlClient;

  namespace ConnectDB

  {

  class Program

  {

  static void Main(string[] args)

  {

  SqlConnection sqlCon = new SqlConnection();

  sqlCon.ConnectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI";

  sqlCon.Open();

  SqlCommand sql_Command = new SqlCommand();

  sql_Command.CommandText = "Query Text";

  sql_Command.Connection = sqlCon;

  SqlDataAdapter dbAdapter = new SqlDataAdapter(sql_Command);

  DataSet dbSet = new DataSet();

  dbAdapter.Fill(dbSet);

  //dbSet.GetXml();

  Console.WriteLine(dbSet.GetXml()。ToString());

  sqlCon.Close();

  Console.Read();

  }

  }

  }

人气教程排行