如何从零开始创建数据库? [英] How to create a database from nothing?

查看:95
本文介绍了如何从零开始创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应该处理联系人,订单等的应用程序。

我只想分发exe文件并让应用程序花费

来创建并且更改要使用的数据库。

此外,我还想让用户可以选择要创建和使用哪种数据库, MSSQL,Oracel,Access




这可能吗?或者我也分发了数据库文件?我的第一个想法是使用OLEDB并让用户选择使用哪个提供商,但是我没有有效的数据库就无法打开数据库连接

文件。


与此类似的东西..


假设字符串aConnectionString等于

" Provider = Microsoft.Jet.OLEDB.4.0; Data Source = \ somepath \ mydb.mdb; User

Id = admin; Password =;"

或Provider = sqloledb; Data Source = Aron1; Initial

Catalog = pubs; Integrated Security = SSPI;"

或Provider = MySQLProv; Data Source = mydb; User

Id = UserName; Password = asdasd;"


OleDbConnection oConnection = new OleDbConnection(aConnectionString);

OleDbCommand oCommand = new OleDbCommand(" create database

aDbName",oConnection);

oConnection.Open();

oCommand。 ExecuteNonQuery();

oConnection.Close();


Br

Jon as

I''m creating a application that should handle contacts, orders etc.
I want to distribute only the exe file and let the application take
care of creating and changing the database that is to be used.
Furthermore, I would also like to make it possible for the user to
select which sort of database to create and use, MSSQL, Oracel, Access
etc.

Is this possible? Or do I have distribute a databas file as well? My
first idea was to use OLEDB and let the user select which provider to
use, but I can''t open a databas connection without a valid database
file.

Something simular to this..

Assume that the string aConnectionString would equal
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\somepath\mydb.mdb;User
Id=admin;Password=;"
or "Provider=sqloledb;Data Source=Aron1;Initial
Catalog=pubs;Integrated Security=SSPI;"
or "Provider=MySQLProv;Data Source=mydb;User
Id=UserName;Password=asdasd;"

OleDbConnection oConnection = new OleDbConnection(aConnectionString);
OleDbCommand oCommand = new OleDbCommand("create database
aDbName",oConnection);
oConnection.Open();
oCommand.ExecuteNonQuery();
oConnection.Close();

Br
Jonas

推荐答案

Jonas,


下面的VBNET中的完整示例,设置它不是那么难在C#

语言。


我希望这有帮助吗?


Cor


Public Class Main

Public Shared Sub Main()

Dim catNewDB As New ADOX.Catalog

Dim fi As New IO。 FileInfo(" c:\ db1.mdb")

if fi.Exists Then

If MessageBox.Show(" Delete?"," Existing File) db1.mdb",_

MessageBoxButtons.YesNo)= DialogResult.Yes然后

fi.Delete()

否则

退出Sub

结束如果

结束如果

catNewDB.Create(" Provider = Microsoft.Jet.OLEDB.4.0; " &安培; 数据

来源= C:\db1.mdb)

''制作表我们使用Adonet

Dim conn As New OleDb.OleDbConnection(" Provider = Microsoft.Jet.OLED B.4.0;&

_

" Data Source = C:\ db1.mdb ; User Id = admin; Password =;")

Dim cmd As New OleDb.OleDbCommand(" CREATE TABLE人员(& _

" AutoId) int identity,"& _

" Id int NOT NULL,"& _

" Name NVarchar(50),"& _

" BirthDate datetime,& _

" IdCountry int,"& _

" CONSTRAINT [pk_AutoId] PRIMARY KEY(AutoId))",conn)

conn.Open()

尝试

cmd.ExecuteNonQuery()

Catch ex As OleDb.OleDbException

MessageBox.Show(ex.Message," OleDbException")

退出Sub

Catch ex作为例外

MessageBox.Show(ex.Message," GeneralExcepti on)

退出Sub

结束尝试

cmd =新OleDb.OleDbCommand(" CREATE TABLE countries(") &安培; _

" AutoId int identity," &安培; _

" Id int NOT NULL," &安培; _

" Name NVarchar(50)," &安培; _

" CONSTRAINT [pk_AutoId] PRIMARY KEY(AutoId))",conn)

尝试

cmd.ExecuteNonQuery()

Catch ex As OleDb.OleDbException

MessageBox.Show(ex.Message," OleDbException")

退出Sub

Catch ex As Exception

MessageBox.Show(ex.Message," GeneralException")

退出Sub

结束尝试

conn.Close()

结束次级

结束班级


Cor
Jonas,

Complete sample in VBNET below, it cannot be that hard to set that in C#
language.

I hope this helps?

Cor

Public Class Main
Public Shared Sub Main()
Dim catNewDB As New ADOX.Catalog
Dim fi As New IO.FileInfo("c:\db1.mdb")
If fi.Exists Then
If MessageBox.Show("Delete?", "Existing File db1.mdb", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
fi.Delete()
Else
Exit Sub
End If
End If
catNewDB.Create("Provider=Microsoft.Jet.OLEDB.4.0; " & "Data
Source=C:\db1.mdb")
''To make tables we use Adonet
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLED B.4.0;" &
_
" Data Source=C:\db1.mdb;User Id=admin;Password=;")
Dim cmd As New OleDb.OleDbCommand("CREATE TABLE persons ( " & _
"AutoId int identity ," & _
"Id int NOT NULL," & _
"Name NVarchar(50)," & _
"BirthDate datetime," & _
"IdCountry int," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
conn.Open()
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
cmd = New OleDb.OleDbCommand("CREATE TABLE countries ( " & _
"AutoId int identity ," & _
"Id int NOT NULL," & _
"Name NVarchar(50)," & _
"CONSTRAINT [pk_AutoId] PRIMARY KEY (AutoId)) ", conn)
Try
cmd.ExecuteNonQuery()
Catch ex As OleDb.OleDbException
MessageBox.Show(ex.Message, "OleDbException")
Exit Sub
Catch ex As Exception
MessageBox.Show(ex.Message, "GeneralException")
Exit Sub
End Try
conn.Close()
End Sub
End Class

Cor


Patty,


通常感谢我在这个样本之上


设置对COM adox ext 2.x的引用为dll和安全


应该这样做。


这一切。


Cor
Patty,

Thanks normally I have above this sample

set a reference to COM adox ext 2.x for dll and security

That should be done for this.

That all.

Cor


有一篇知识库文章显示如何弹出数据链接属性

对话框让用户选择数据库连接:

http://support.microsoft.com/default...b;EN-US;310083
< br $> b $ b -

Scott
http: //www.OdeToCode.com


周五,2004年8月27日08:57:07 -0700,Patty O'Dors

<帕******** @ discussions.microsoft.com>写道:
There is a KB article showing how to pop up the Data Link Properties
dialog to let the user select the database connection:

http://support.microsoft.com/default...b;EN-US;310083

--
Scott
http://www.OdeToCode.com

On Fri, 27 Aug 2004 08:57:07 -0700, Patty O''Dors
<Pa********@discussions.microsoft.com> wrote:
您制作SQL越通用,就越有可能与所有数据库提供商合作。
例如,
create table
将与Oracle,SQL或Access一起使用 - 或几乎任何其他东西。
但是一些功能如IIf,CStr等。只能使用Access。
像聚簇索引这样的东西不适用于Access。
有些东西只适用于MSSQL,没有别的东西,有些东西可以和Oracle一起工作,没有别的。
所以,如果你希望他们能够选择,让他们知道他们可以选择哪一个,并确保你已经在*所有这些中进行了测试。
然后在应用程序开始时,您可以弹出一个数据库连接对话框(其目的是提示用户输入并返回一个
连接字符串),类似于管理工具中的一个\ dat数据来源于Windows中的applet。如何做到这一点是另一篇文章的主题 - 我不确定你是如何做到这一点但我很确定你可以做到这一点,而不是自己滚动。
The more generic you make the SQL, the more likely it will be to work with
all database providers.
For instance,
create table
will work with either Oracle, SQL or Access - or pretty much anything else.
But some functions like "IIf", "CStr" will only work with Access.
Some things like clustered indexes will NOT work with Access.
Some things will only work with MSSQL and nothing else, and some things will
work with Oracle and nothing else.
So if you want them to be able to choose, let them know which they can
choose from, and make sure that you''ve tested it on *all* of those.
Then at the start of the application, you can pop up a "database connection"
dialog, (the purpose of which is to prompt the user for input and get back a
connection string), similar to the one in the administrative tools\data
sources applet in windows. How to do that is a subject for another post - I''m
unsure as to how you do it exactly but I''m pretty sure you can do it without
rolling your own.






这篇关于如何从零开始创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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