日期:2014-05-17  浏览次数:20543 次

菜鸟求助 关于VS2010中数据库的连接问题
我是一个刚刚接触VS2010的菜鸟,前不久才安装好,现在想请教大神们,怎么从VS2010刚安装好的状态开始去,一步一步连接数据库,因为本人比较笨,网上各种别人说的都看不怎么懂,非常希望有很好的很详细的方法来教我。不要太抽象了,谢谢各位了。。。。。

------解决方案--------------------
直接引用DbHelper类,在web.config里加连接字符串。
------解决方案--------------------
下面是最“傻瓜式”的连接数据库方式。在这个例子里,数据库名字是Demo,登陆MS SQL Server的账号名是sa,密码是123456。 【你改成你自己的】
当然还有其他方式,比如通过配置文件等。但万变不离其中,相信你可以搞定。

<textarea name="code" class="c#"> 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace DatabaseConnection
{
class Program
{
static void Main(string[] args)
{

string connectionStr = "server=127.0.0.1;database = Demo;uid = sa; pwd = 123456";
SqlConnection connection = new SqlConnection(connectionStr);
connection.Open();
// 上面三行就已经连接到数据库Demo了。下面是对Demo里名叫Genres表的操作(这里只是举个例子)

string sqlStr = "select * from Genres";
SqlCommand command = new SqlCommand(sqlStr,connection);
SqlDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Console.WriteLine(dataReader.GetInt32(0)+","+dataReader.GetString(1));
}
}
}
}

------解决方案--------------------
楼主找本书啃啃,什么都知道了