程序链接到同一文件夹数据库 [英] Program linked to same folder database

查看:64
本文介绍了程序链接到同一文件夹数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,CodeProject社区!



我目前正在处理我的项目,我需要使用SqlConnection类将我的数据库链接到我的项目。在这里,我需要你的帮助:我如何编写数据库的地址,使程序只使用与它在同一文件夹中的数据库。我试图删除E:\Programs\C#Programs\Atestat来自AttachDbFilename = E:\Programs\C#Programs\Atestat\Atestat\elevi.mdf,但它会给我一个错误。



我非常感谢每一个答案。



祝你好运!





EDIT1:关于现在给出的所有答案,我非常感谢你的暗示,但我担心这两个答案都不会解决我的问题。

原因是我需要在CD上进一步传输程序。这是为了证明我对C#的了解。在给出这些条件的情况下,我的程序的范围是从数据库中提取,更新和插入+显示一些数据,无论这个程序在哪里将被找到。 (我正在使用Microsoft Visual c#2008)

解决方案

不要。

如果你坚持认为数据库必须在同一个文件夹中作为应用程序,然后在生产中,您的应用程序很有可能会失败,因为大多数安装将应用程序置于Program Files文件夹下,默认情况下不启用写入以减少病毒活动。



这意味着当您第一次尝试INSERT或UPDATE数据库时,您的应用程序将因访问冲突异常而崩溃。把它放在一个明智的地方:我应该在哪里存储我的数据? [ ^ ]将告诉你在哪里 - 如果你使用一个特殊文件夹和应用程序GUID作为包含文件夹,用户也很难干预。


我会建议你不要首先使用通过连接字符串附加数据库-feature。



原因1:它仅支持SQL-Server Express版本。如果您希望您的应用程序能够使用更高版本的SQL-Server,它无论如何都无法工作。



原因2:Microsoft建议停止使用此功能是因为他们正在考虑在将来的版本中将其删除。



替代方案完全无关紧要:使用SQL Server Management Studio附加数据库,使其成为常规数据库,永久数据库。如果您想将项目移动到其他PC,您可以将其拆下或进行备份,然后再将其连接到另一台PC上。



编辑问题更新后:



可以通过编程方式附加数据库(.mdf文件)。也就是说,您可以让您的应用程序执行我在上面段落中提到的内容。结合我不使用您当前方法的论点,这只能加强您证明您对C#,.NET和SQL-Server的了解。



请请参阅此Codeproject文章:

以编程方式枚举,附加,和分离SQL Server数据库 [ ^ ]

如果需要,您可以通过此Google搜索找到有关该信息的更多信息:

https://www.google.com/search?q=sql+server+attach+database+programmatically [<一个href =https://www.google.com/search?q=sql+server+attach+database+programmaticallytarget =_ blanktitle =新窗口> ^ ]


<块引用> 中的空格AttachDbFilename = E:\Programs\C#Programs\Atestat\Atestat\elevi.mdf是一个问题



连接字符串的问题在于它们必须在字符串和代码中都有意义。



字符串只是一个字符串,对吗?



嗯,是的,不是。您的程序将不知道字符串中的语法是否正确。它只会说是 - 这是一个字符串。



字符串解析器不知道参数何时开始或结束。当然,'='之后的所有内容都是参数值,但它在哪里结束?



答案是什么?空白!

对于解析器,它看起来像这样:AttachDbFilename ='E:\Programs\C#'blahblahblah



为了解决这个问题,解析器接受单引号作为字符串:



AttachDbFilename ='E:\Programs\C#Programs \ Atestat \ Atestat \ elevi.mdf'



试试看,告诉我。


Good day, CodeProject Community!

I am currently working on my project and i need to link my database to my project by using the SqlConnection class. Here I need your help: How do I write the address of the database in such way that the program will only use the database that is in the same folder as it. I've tried to delete the "E:\Programs\C# Programs\Atestat" from "AttachDbFilename=E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf ", but it would give me an error.

I'd greatly appreciate every answer.

Best regards!


EDIT1: Regarding all the answers given by now, I am very thankful for your implication, but I am afraid that neither of the answers would solve my problem.
The reason is that i need to transfer the program further on a CD. It is made to
prove my knowledge about C#.Having these conditions given, the scope of my program is to extract, update and insert + show some data from/into a database, no matter where this program will be located. ( I am using Microsoft Visual c# 2008)

解决方案

Don't.
If you insist that the database must be in the same folder as the application, then in production there is a very good chance your application will fail, as most installations put the app under the "Program Files" folder which is not write enabled by default to reduce virus activity.

Which means that the first time you try to INSERT or UPDATE the DB, you app will crash with an Access Violation Exception. Put it in a sensible place: Where should I store my data?[^] will show you where - and if you use one of the special folders and the Application GUID as the containing folder, it will be difficult for the user to meddle with as well.


I would recommend you to not use the "attach database through connection string"-feature in the first place.

Reason 1: It's only supported for SQL-Server Express editions. If you want your application to be able to use 'higher' editions of SQL-Server, it won't work anyway.

Reason 2: Microsoft recommends to stop using this feature because they are considering to remove it in a future version.

The alternative is completely trivial: Use SQL Server Management Studio to attach your database, making it a regular, permanent database. If you want to move your project to some other PC you can just either detach it or make a backup and attach it again on the other PC.

Edit after the question was updated:

It's possible to attach a database (an .mdf-file) programmatically. That is, you could let your application perform what I've mentioned in the paragraph above. In combination with my arguments for not using your current approach this could only serve to strengthen your point of proving your knowledge about C#, .NET and SQL-Server.

Please refer to this Codeproject-article:
Programmatically Enumerating, Attaching, and Detaching SQL Server Databases[^]
And, if needed, you could find further information on that with this Google search:
https://www.google.com/search?q=sql+server+attach+database+programmatically[^]


The space in "AttachDbFilename=E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf " is an issue

The problem with connectionstrings is that they have to make sense within the string as well as in your code.

A string is just a string, right?

Well, yes and no. Your program will not know if the syntax within the string is correct. It will just say "yup - that's a string".

The string parser doesn't know when a parameter starts or ends. Sure, everything after the '=' is the parameter value but where does it end?

The answer? Whitespace!
To the parser, it looks like this: "AttachDbFilename='E:\Programs\C#' blahblahblah"

To get around this, the parser accepts single quotes as strings:

"AttachDbFilename='E:\Programs\C# Programs\Atestat\Atestat\elevi.mdf'"

Try it and let me know.


这篇关于程序链接到同一文件夹数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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