问题描述
- 新手求助:vstudio c#2010如何连接SQL2008数据库
-
在SQL server2008上创建的数据库,使用Vstudio c#编写一个小的管理系统,但是一直无法连接数据库,
解决方案
http://download.csdn.net/detail/xianfajushi/4028787
解决方案二:
你是怎么连接的? 帖一下你的连接字符串和报错信息
不然神也帮不了你, 不会提问
解决方案三:
无非就是这么几个问题,一个是提示无法找到实例名,这个检查sql server有没有装好,连接字符串的实例名有没有指定对。要么你使用ip或者主机名访问的,那么检查tcp/ip协议和远程访问有没有启用,防火墙。还有就是sa密码无效,那么看看你的密码是否正确,有没有启用sa用户。
解决方案四:
打开vs的服务器资源管理器,然后创建新的数据库连接,按要求输入数据库位置、账户、密码登录,如果登录成功,右键数据库属性处就有连接字符串,复制粘贴到Config中;
解决方案五:
To create a data connection to a SQL Server database
In Server Explorer/Database Explorer click Connect to Database.
In the Choose Data Source dialog box, select Microsoft SQL Server, and then click OK.
If the Add Connection dialog box opens, and the Data source is not Microsoft SQL Server, click Change to open the Choose/Change Data Source dialog box. For more information, see Choose/Change Data Source Dialog Box.
Select a server name from the drop-down list, or type the name of the server where the database you want to access is located.
Based on the requirements of your database or application, select either Windows Authentication or use a specific user name and password to log on to the SQL Server (SQL Server Authentication). For more information, see Add/Modify Connection (Microsoft SQL Server).
Select the database you want to connect to from the drop-down list.
Click OK.
The msdn link is http://msdn.microsoft.com/en-us/library/s4yys16a%28v=vs.90%29.aspx
i hope my answer will help.
解决方案六:
首先添加引用命名空间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();?????????}
时间: 2025-01-30 15:40:51