您的位置:首页 > 数据库

有关vs2008连接数据库的一些操作

2012-07-09 13:59 405 查看
有关连接字符串

基本上就已经完成了SQL软件方面的设置了,接下来的是连接字符串:

我这边用的是这个串:

Server=127.0.0.1,3725;Initial Catalog=ITNB;User ID=sa;Password=sa123456

当然网络上也发现了很多,这些都是和你的SQL版本相关的,也一并列出来:

Data Server=.\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa123456

Data Server=服务器名\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa123456

Data Server=localhost\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa123456

Data Server=.;Initial Catalog=Northwind;User ID=sa;Password=sa123456

Data Server=服务器名;Initial Catalog=Northwind;User ID=sa;Password=sa123456

设置web.config文件:

在应用程序中的web.config文件添加如下数据库连接的配置:

<connectionStrings>

<add name="ConnectionSqlServer"

connectionString="Data Source=

.\SQLEXPRESS;Initial Catalog=Northwind;

User ID=sa;Password= sapassSql" providerName="System.Data.SqlClient"/>

</connectionStrings>

连接sqlserver用sqlconnection

先导入命名空间

using System.Data.Sqlclient;

using(SqlConnection con = new SqlConnection())

{

con.ConnectionString = "连接字符串";

SqlCommand cmd = new SqlCommand();

cmd.Connection = con;

cmd.CommandText = "查询语句";

//读取数据库内容

SqlDataAdapter adapter = new SqlDataApater();

DataSet ds = new DataSet();

adapter.Fill(ds);

//绑定数据源

DataGridView1.DataSource = ds.Tables[0];

}

1.开启SQL2005远程连接功能,开启办法如下:配置工具->SQLServer外围应用配置器->服务和连接的外围应用配置器->打开MSSQLSERVER节点下的DatabaseEngine节点,先择“远程连接”,接下建议选择“同时使用TCP/IP和namedpipes”,确定后重启数据库服务就可以了。 2.登陆设置改为:SQLServer和Windows身份验证模式,具体设置如下:
SQLServerManagementStudio管理器->Windows身份验证连接服务器->对象资源管理器中选择你的数据服务器->右键->属性->安全性->SQLServer和Windows身份验证模式选中。 3.设置一个SQLServer方式的用户名和密码:具体设置如下: (1)SQLServerManagementStudio管理器->Windows身份验证连接服务器->对象资源管理器中选择你的数据服务器->展开服务器上的“安全性”->登陆名->在sa帐号上点右键->“选择页”选择常规->更改sa登陆帐号的密码。这样就设置了一个用户名为sa,密码为

a123456的用户。
(2)“选择页”选择状态->登陆修改为启用。 4.数据库连接字符串:数据库连接字符串有好多种:
DataServer=.\SQLEXPRESS;InitialCatalog=Northwind;UserID=sa;Password=sa123456 DataServer=服务器名\SQLEXPRESS;InitialCatalog=Northwind;UserID=sa;Password=sa123456 DataServer=localhost\SQLEXPRESS;InitialCatalog=Northwind;UserID=sa;Password=sa123456 DataServer=.;InitialCatalog=Northwind;UserID=sa;Password=sa123456 DataServer=服务器名;InitialCatalog=Northwind;UserID=sa;Password=sa123456 ……

那种正确,这跟数据库版本有关系,如果是SQLServer2005Express版本,则必须要有“\SQLEXPRESS”。而且如果这个字符串是定义为一个变量的时候,VS2005还会在“\”的下面加个红色的波浪线提示你“\S是无法识别的转义序列”,因此如果字符串是定义为一个变量的时候应该写成Server=.\\SQLEXPRESS。 5.注册SQLServer数据库:在路径“C:\Windows\Microsoft.NET\Framework\v2.0.50727”下运行“ASPNET_REGSQL”指令,就会出现ASP.NETSQLServerSetupWizard向导,连续按下两个下一步后就会出现SQLServer注册界面,填入你要注册的数据库参数就注册好了。注册完后会在你的数据库里多几个表(如图): 6.设置数据库连接字符串:打开IIS->在默认网站或是网站所在的虚拟目录点击右键选择属性->选择ASP.NET选项卡->编辑配置->在“常规”页签编辑“LocalSqlServer”数据库连接字符串:
DataServer=.\SQLEXPRESS;InitialCatalog=Northwind;UserID=sa;Password=sa123456

7.设置web.config文件:在web.config文件添加如下程序:
<connectionStrings><addname="LocalSqlServer"connectionString="DataSource= .\SQLEXPRESS;InitialCatalog=Northwind; UserID=sa;Password=sa123456" providerName="System.Data.SqlClient"/></connectionStrings>


首先添加引用命名空间using System.Data.SqlClient;

然后建立连接 string conStr= "server=.;database=yourDB;uid=sa;pwd=sa";

先用SqlConnection建个连接

然后创建SqlCommand对象

利用 SqlDataReader 来读取数据

举个例子

string sql = "select bookName from booklist where id='" + id+ "'";

SqlConnection con = new SqlConnection();//查询语句

con.ConnectionString = conStr;

try

{

con.Open();

SqlCommand cmd = new SqlCommand(sql, con);

SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())

{

//执行语句

}

else

{

//执行语句

}

dr.Close();

}

catch (Exception e)

{

//执行语句

}

con.Close();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: