展会信息港展会大全

C#连接SQL Server的常用字符串
来源:互联网   发布日期:2016-02-17 09:43:58   浏览:2590次  

导读: 一:C#连接SQL数据库 DataSource=myServerAddress;Initial Catalog=myDataBase;UserId=myUsername;Passwor...

一:C#连接SQL数据库

DataSource=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;

DataSource=190.190.200.100,1433;NetworkLibrary=DBMSSOCN;InitialCatalog=myDataBase;UserID=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;IntegratedSecurity=SSPI;

1:Integrated Security参数

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

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

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

DataSource=myServerAddress;Initial Catalog=myDataBase;IntegratedSecurity=SSPI;

DataSource=myServerAddress;Initial Catalog=myDataBase;IntegratedSecurity=true;

DataSource=myServerAddress;InitialCatalog=myDataBase;;UserID=myUsername;

Password=myPasswordIntegratedSecurity=false;

2:参数Trusted_Connection

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

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

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;IntegratedSecurity=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;IntegratedSecurity=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;InitialCatalog=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();

}

}

}

赞助本站

人工智能实验室

相关热词: 开发 编程 android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港