SQL Server 2008与C#应用程序的连接 [英] SQL server 2008 connectivity with C# application

查看:87
本文介绍了SQL Server 2008与C#应用程序的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个'菜鸟'问题:)

我想在C#中用SQL Server 2008中的数据库编写一个应用程序。



应用程序将保存在数据库日志,设置,用户帐户中..



我想问你关于应用和数据库之间的连接。

你能推荐我一个示例项目吗?

我的意思是 - 我知道如何使用ADO.NET,如何连接,发送命令......我只需要一个例子 - 看看它在解决方案中的外观。



感谢您的时间。



------------- -------------------------------------------------- --------------

编辑:

也许这听起来很奇怪,因为在顶部我写了'菜鸟',但是:



我不是为初学者寻找样品,我想看看'亲'的灵魂。

我的意思是......我正在寻找一些形式 - 这是专业程序员在专业应用程序中使用的。



例如 - 创建名为'Data'的项目,创建类DatabaseProvider.cs或DataRepository.cs或QueryRepository.cs ..保持连接字符串的位置。我不知道。



也许我会这样问:我正在寻找模式,如何在我的程序中创建'数据'项目(类库)解决方案,它将负责与SQL Server 2008的连接。



再次感谢



< br $> b $ b

----------------------------------- ------------------------------------------

编辑2:

因此,这将是Windows服务应用程序,它将作为WCF服务的主机。 Windows服务应用程序必须从数据库获取数据到配置,保存日志,存储帐户数据..以及存储其他一些东西。



我的第一个想法是创建单独的项目:< solutionname> .Data,并在内部创建类:

DatabaseProvider.cs - in在这里,我将访问数据库,插入,更新或选择数据。

QueryRepository.cs - 在数据库提供程序中使用的已定义查询



IDataRepository.cs - 整个解决方案中用于访问数据库的方法(例如, GetSettings();)

DataRepository.cs - 定义的IDataRepository.cs

例如。

public int GetSettings()

{

使用(DatabaseProvider db = new DatabaseProvider())

{

返回db.GetSettings();

}

}

和带连接字符串的App.config。



在DatabaseProvider.cs中我使用ADO.NET访问数据库。

但是,现在我不确定,如果我能够在此表上插入操作后获得SysId(主键,表中的列)。







现在我正在阅读设计数据层组件并通过层传递数据一文(但是 - 它已经过时 - 所以我不确定解决方案现在是否是最好的。)

Hi,
I've got a 'rookie' question :)
I want to write an application in C# with database in SQL Server 2008.

The application will save in database logs, settings, users accounts..

I want to ask you about the connection between app and DB.
Could you recommend me a sample project ?
I mean - I know how to use ADO.NET, how to connect, send command... I just need an example - to see how it looks in the solution.

Thanks for your time.

-----------------------------------------------------------------------------
edit:
Maybe this sounds wierd, because at the top I wrote 'rookie', but:

I'm not looking for samples for beginners, I would like to look at 'pro' soultion.
I mean.. I'm looking form some pattern - which profesjonal programmers use in profesjonal applications.

For example - create project named 'Data', create class DatabaseProvider.cs or DataRepository.cs or QueryRepository.cs.. where to keep connection string. I don't know.

Maybe I shound ask like that: I'm looking for pattern, how to create 'Data' project (class library) in my program solution, which will be responsible for connectivity with SQL server 2008.

Thanks, once again



-----------------------------------------------------------------------------
EDIT 2:
So, this will be windows service application, which will be host for WCF sevices. Windows service application have to get data from database to configuration, save logs, store accounts data.. and store some other things.

My first thought was to create separate project: <solutionname>.Data, and inside create classes:
DatabaseProvider.cs - in here, i will access database, insert, update or select data.
QueryRepository.cs - defined queries, used in database provider

IDataRepository.cs - methods used in whole solution to access database (eg. GetSettings();)
DataRepository.cs - defined IDataRepository.cs
eg.
public int GetSettings()
{
using (DatabaseProvider db = new DatabaseProvider())
{
return db.GetSettings();
}
}
and App.config with connection string.

In DatabaseProvider.cs I use ADO.NET to access database.
But, now I'm not sure, if I will be able to get SysId (primary key, column in table), after insert operation on this table.



Now I'm reading "Designing Data Tier Components and Passing Data Through Tiers" article (however - it is outdated - so I'm not sure if the solutions are the best right now).

推荐答案

查看编辑块,设计数据的设计层组件应该真正取决于您的系统要求,没有一个设计/模式适合所有。但是,以下链接应该有很大帮助:



设计数据层组件并通过层传递数据 [ ^ ]



此外,应考虑和评估诸如实体框架,数据访问应用程序块等技术的选择。



---------------------------------- ------

给你:



参考这些链接

为初学者使用ADO.NET [ ^ ]



ADO.NET代码示例 [ ^ ]



ADO.NET示例应用程序 [ ^ ]
Looking at the edit block, the desing of your data tier components should really depend on your system requirements, there is no one design/patterns fits all. However, following link should be of great help:

Designing Data Tier Components and Passing Data Through Tiers[^]

Also, choice of technologies such as Entity Framework, Data Access Application block should be considered and evaluated.

----------------------------------------
Here you go:

Refer to these links
Using ADO.NET for beginners[^]

ADO.NET Code Examples[^]

ADO.NET Sample Application[^]


检查这些:



初学者访问S指南QL Server通过C# [ ^ ]



http://forums.asp.net/t/1193643.aspx/1?How+To+Connect+to+SQL+using+C+and+SELECT+INSERT+更新+ [ ^ ]



希望这会有所帮助:)
check these out:

Beginners guide to accessing SQL Server through C#[^]

http://forums.asp.net/t/1193643.aspx/1?How+To+Connect+to+SQL+using+C+and+SELECT+INSERT+UPDATE+[^]

hope this helps :)


我建议你使用 Enterprise Library 5.0 数据应用程序块



除此之外,您还可以使用 codeplex 网站上提供的一些优质模式块。



谢谢

Subhendu
I would suggest you to use Enterprise Library 5.0 Data Application Block

Other than that, you can use some good pattern blocks available in codeplex site.

Thanks
Subhendu


这篇关于SQL Server 2008与C#应用程序的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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