如何检查vb .net项目中sql server中是否存在数据库和表? [英] How to check if a database and tables exist in sql server in a vb .net project?
问题描述
有谁能告诉我如何从vb.net项目检查sql server中是否存在数据库和表?我想要做的是检查数据库是否存在(最好是在'If'语句中,除非有人有更好的方法)并且如果它确实存在我会做一件事,如果它不存在我创建数据库与表和列。任何有关此事的帮助将不胜感激。
编辑:
该应用程序已连接到一台服务器。当应用程序在PC上运行时,我希望它检查数据库是否存在,如果存在,那么它会继续并执行它应该执行的操作,但是如果数据库不存在则会先创建数据库,然后继续执行应该怎么做。所以基本上我希望它在第一次在PC上运行时创建数据库然后继续它的业务,然后每次它在PC上运行之后,我希望它看到数据库存在然后继续它的业务。我想要这样的原因是因为这个应用程序将在多台PC上,我只想创建一次数据库和表(第一次在PC上运行),然后当它在不同的PC上运行时,它看到数据库已经存在,然后使用在另一台PC上创建的现有数据库运行应用程序。
检查此代码项目文章
检查是否数据库中存在一个表或字段 [ ^ ]
和这篇帖子here
尝试打开与服务器的连接。如果它导致你知道的异常。
使用 oSqlConnection As 新 SqlConnection( ConnectionString)
oSqlConnection.Open()
结束 使用
)
检查桌子。
SELECT *
FROM INFORMATION_SCHEMA.TABLES T
WHERE T.TABLE_NAME LIKE ' %MY_TABLE%'
将所有内容放在VB中。
使用 oSqlConnection 作为 新 SqlConnection(My.Settings.ProductTraxConnectionString)
oSqlConnection.Open()
使用 oQuery As 新 SqlCommand(
SELECT COUNT(*)&
FROM INFORMATION_SCHEMA.TABLES T&
WHERE T.TABLE_NAME LIKE @LookupTable,oSqlConnection)
oQuery。 Parameters.AddWithValue( @ LookupTable, %MY_TABLE%)
Dim oResult < span class =code-keyword> as object = oQuery.ExecuteScalar()
End 使用
结束 使用
如果您正在使用sql server以下查询
select * from sys .Databases其中Name ='urDataBaseName'
获取数据库详细信息
用于检查表格那个DataBase
使用urDataBaseName;
select * f rom TableName
让我知道这是你认为你想的问题。
快乐编码: - )
Can anyone tell me how I would go about checking if a database and tables exists in sql server from a vb.net project? What I want to do is check if a database exists (preferably in an 'If' statement, unless someone has a better way of doing it) and if it does exist I do one thing and if it doesn't exist I create the database with the tables and columns. Any help on this matter would be greatly appreciated.
Edit:
The application has a connection to a server. When the application runs on a PC I want it to check that a database exists, if one exists then it goes and does what it's supposed to do, but if a database does NOT exist then it creates the database first and then goes on to do what it's supposed to do. So basically I want it to create the database the first time it runs on a PC and then go about it's business, then each time it runs on the PC after that I want it to see that the database exists and then go about it's business. The reason I want it like this is because this application will be on more than one PC, I only want the database and tables created once, (the first time it runs on a PC) and then when it runs on a different PC, it sees that the database already exists and then run the application using the existing database created on the other PC.
Check this codeproject article
Check if a table or field exists in a database[^]
and this post here
Try to open the connection to the Server. If it causes an exception you know.
Using oSqlConnection As New SqlConnection("ConnectionString") oSqlConnection.Open() End Using )
To check for a table.
SELECT * FROM INFORMATION_SCHEMA.TABLES T WHERE T.TABLE_NAME LIKE '%MY_TABLE%'
Putting it all together in VB.
Using oSqlConnection As New SqlConnection(My.Settings.ProductTraxConnectionString) oSqlConnection.Open() Using oQuery As New SqlCommand( "SELECT COUNT(*) " & "FROM INFORMATION_SCHEMA.TABLES T " & "WHERE T.TABLE_NAME LIKE @LookupTable", oSqlConnection) oQuery.Parameters.AddWithValue("@LookupTable", "%MY_TABLE%") Dim oResult as object = oQuery.ExecuteScalar() End Using End Using
If you are using sql server the following query
select * from sys.Databases where Name='urDataBaseName'
Gets you the database details
For Checking the Table in that DataBase
Use urDataBaseName;
select * from TableName
Let Me Know this was the Think u was the think you asking.
Happy Coding :-)
这篇关于如何检查vb .net项目中sql server中是否存在数据库和表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!