C#.NET和SQL 2008. [英] C#.NET and SQL 2008.

查看:86
本文介绍了C#.NET和SQL 2008.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释如何在C#Windows应用程序中连接到SQL数据库(扩展名为.mdf文件)吗?任何答案表示赞赏!

注意:如果我不清楚我要问的是什么,或者您想了解更多信息,请告诉我.

Thx!

Can someone please explain to me how to connect to a SQL database (with the .mdf file extension) in a C# windows application? Any answers are appreciated!

Note: if I am not too clear on what I am asking, or you want more information, just let me know.

Thx!

推荐答案

使用LINQ连接到数据库

步骤

1.为您的数据库创建模式.

下载SQLMETAL工具并为您的数据库制作模式文件

SQL METAL命令生成shcema:
Connect to DB using LINQ

Steps

1. Create the schema for your database.

Download the SQLMETAL tool and make the schema file for your database

SQL METAL Command to generate shcema:
<location>\SqlMetal.exe /server:serverName /database:databaseName /functions /sprocs 
   /language:cs /code:<location to="" generate="" schema="">\schemaFile.cs /namespace:nameSpaceName /pluralize</location></location>



有关SQL金属的更多选项,请转到 http://msdn.microsoft. com/en-us/library/bb386987(v = VS.90).aspx [



For more options of the SQL metal please goto http://msdn.microsoft.com/en-us/library/bb386987(v=VS.90).aspx[^]


2. Once the Schema is generated, attach it to your project.

3. While your application on load, connect to database using connection string.

DatabaseName _db = new DatabaseName(@"Persist Security Info=False" +
                                     ";Integrated Security=" + _integratedSecurity + 
                                     ";User ID=" + _userName +
                                     ";Initial Catalog=" + _dbIdentification +
                                     ";Data Source=" + _serverName +
                                     ";Password=" + _password);



4.然后,您可以使用"_db"对象访问数据库.



4. Then you can use the ''_db'' object to access the database.

_db.Products.Single(a => a.Name == productName)


转到Visual Studio中的数据"菜单-> ;添加新的数据源->数据库->数据集->新建连接.
然后,您可以找到您的SQLServer并设置密码等.

完成后,您的解决方案资源管理器中将具有一个扩展名为.xsd的数据集.

双击它,您可以直观地看到您选择的数据库.

我想这应该让您开始吗?
Go to the Data menu in Visual Studio->Add new Datasource->Database->DataSet->New connection.
Then you can locate your SQLServer and set password and so on.

When you are done you will have a DataSet in your Solution Explorer with the .xsd extension.

Double click on it and you have a visual representation of your Chosen DataBase.

This should get you started I think?


转到此链接:thumbsup:
使用C#.NET连接到SQL Server Express数据库[ ADO.NET概述 [
go this links :thumbsup:
Connecting to a SQL Server Express Database with C# .NET[^]
Overview of ADO.NET[^]


这篇关于C#.NET和SQL 2008.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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