C#连接SQL Server

by shinichi_wtn 2009-02-04 22:20

首先,确保引用如下两个类库

using System.Data;
using System.Data.SqlClient;

连接本机SQL服务器

string strConnection = "Trusted_Connection=SSPI;";
strConnection += "Database=数据库名称;Server=(local)\\SQLEXPRESS;";
//如果数据库设置了实例名,则需要填写相应实例名
strConnection += "Connect Timeout=30";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();

连接远程SQL服务器

string strConnection ="user id=sa;password=11";
strConnection+="initial catalog=数据库名称;Server=数据库服务器地址;";
strConnection+="Connect Timeout=30";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.open();

之后就是执行Command返回SQL查询结果了

Tags: ,

C#

(仅用于Gavatar)

  Country flag

biuquote
  • Comment
  • Preview
Loading

About

shinichi_wtnI'm Shinichi_wtn

Software Engineering Manager at Microsoft

[More...]

Recent Tweets

Twitter May 4, 23:00
因为疫情,五一只能在周边爬山,天气不错 https://t.co/ZJxRHj6fux

Twitter April 17, 22:20
坡峰岭爬山 https://t.co/gevMr3dkop

Twitter March 26, 23:21
月球陨落,剧情不错,特效牛逼 https://t.co/cyoCCXzVFu

Twitter February 1, 00:19
虎年快乐! https://t.co/Dltr5IMfcn

Twitter January 1, 00:05
Happy new year 2022! https://t.co/dCUV2yhO3K

Twitter December 26, 18:18
圣诞节🎄环球城市大道看电影,国产动画《雄狮少年》还是不错的 https://t.co/yVI76EZUVC

Twitter December 24, 22:06
圣诞快乐 https://t.co/SX3QadYBHY

Twitter December 18, 12:16
体验了下联通5g,感觉比移动的5g快不少 https://t.co/KblkbEI99l


Follow me on twitter >>

Month List