如何在Visual Studio 2010中使用数据库 [英] how to use database in visual studio 2010

查看:96
本文介绍了如何在Visual Studio 2010中使用数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
我不知道如何在Visual Studio 2010中连接数据库...

Hello every one...
I dont know how to connect to database in visual studio 2010...

推荐答案

另请参阅以下知识库文章:

http://msdn.microsoft.com/en-us/library/ms171890.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/wxt2cwcc (v = VS.100).aspx [ http://msdn.microsoft.com/en-us/library/fxk9yw1t (v = VS.100).aspx [
Also see the following KB articles:

http://msdn.microsoft.com/en-us/library/ms171890.aspx[^]

http://msdn.microsoft.com/en-us/library/wxt2cwcc(v=VS.100).aspx[^]

http://msdn.microsoft.com/en-us/library/fxk9yw1t(v=VS.100).aspx[^]


假设这是一个SQL数据库,请尝试:
Assuming this is an SQL database, try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }

您将需要指定连接字符串,但是您可以获取那很容易.
1)在VS中打开服务器资源管理器"窗格. 2)查看数据连接"列表.如果您的数据库在那里,请跳至步骤4
3)创建与数据库的连接.
3.1)右键单击数据连接",然后选择添加连接..."
3.2)填写产生的对话框,直到测试连接"按钮起作用-在这里无法为您提供帮助,太多的变量
3.3)单击确定.
4)单击数据库名称以突出显示它.
5)在属性窗格中,您将找到连接字符串-您可以复制并粘贴该字符串,但是最好将其保留在应用程序配置文件中.

You will need to specify the connection string, but you can get that quite easily.
1) Open the Server Explorer pane in VS
2) Look at the Data Connections list. If your data base is there, skip to step 4
3) Create a connection to your database.
3.1) Right Click "Data Connections", and select "Add Connection..."
3.2) Fill in the dialog that results, until the "Test Connection" button works - can''t help you here, way too many variables
3.3) Click OK.
4) Click on the database name to highlight it.
5) In the properties pane, you will find the connection string - you can copy and paste this, but it is a good idea to keep it in the application config file.


与在vs2008中.
使用ado.net语法

希望您正在寻找使用ado.net的方式
its same as in vs2008.
use ado.net syntax

I hope you were asking for the way to use ado.net


这篇关于如何在Visual Studio 2010中使用数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆