Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,

I am creating sql database and tables at runtime in vb.net appliaction
its created but when i reopen the application it shows that sql database
already exists as well as tables.

Please help me, if already exists(Sql database and Tables) then how can
it will go to next step.

Indranil
Posted
Comments
bbirajdar 31-Jan-13 6:37am    
Check if table exists before creating...

Try below code

bool exists;

try
{
    // ANSI SQL way.  Works in PostgreSQL, MSSQL, MySQL.  
    var cmd = new OdbcCommand(
      "select case when exists((select * from information_schema.tables where table_name = '" + tableName + "' or Table_catalog = '" + dbName + "')) then 1 else 0 end");

    exists = (int)cmd.ExecuteScalar() == 1;
}
catch
{
   
}
 
Share this answer
 
v3
As you already aware how to run SQL query from VB.net , I am giving you two queries for checking table existance

SQL
select * FROM sys.databases WHERE name='DB1' -- Replace your database name with DB1


SQL
SELECT * FROM sys.tables WHERE name='TB1' -- Replace your table name with TB1



Below are codes for VB.Net
VB
'CREATING DATABASE......................... 
 Dim conStr As String = "Server=.;Database=;Trusted_Connection = yes;MultipleActiveResultSets=True;" 
 Dim objCon As New SqlConnection(conStr) 
 Dim obj As SqlCommand 
 Dim strSQL As String 
 Dim myReader As SqlDataReader 

  ' Create the database 
 objCon.Open() 
 obj = objCon.CreateCommand() 
  
Try 
 strSQL = "SELECT * FROM sys.databases WHERE name='INDRA'"
 obj.CommandText = strSQL 
 SET myReader=obj.ExecuteReader()
  IF NOT myReader.Read()
'obj.Close()
   strSQL = "CREATE DATABASE INDRA" 
  ' Execute 
   obj.CommandText = strSQL 
  obj.ExecuteNonQuery() 
  MsgBox("Database Created Successfully...", MsgBoxStyle.Information, "Indranil Chatterjee..") 
  END IF 
  Catch ex As Exception 
  MessageBox.Show(ex.Message) 
End Try 

'objCon.Close()


Do the same thing for table creation


Hope this helps if yes then accept and vote this solution otherwise revert back with your queries
--RDB
 
Share this answer
 
v8
Comments
hspl 31-Jan-13 9:53am    
Thanks for your answer but i could not find where i put these queiries in below form1_load event :-

'CREATING DATABASE.........................
Dim conStr As String = "Server=.;Database=;Trusted_Connection = yes"
Dim objCon As New SqlConnection(conStr)
Dim obj As SqlCommand
Dim strSQL As String
' Create the database
objCon.Open()
obj = objCon.CreateCommand()
strSQL = "CREATE DATABASE INDRA"
' Execute
obj.CommandText = strSQL
Try
obj.ExecuteNonQuery()
MsgBox("Database Created Successfully...", MsgBoxStyle.Information, "Indranil Chatterjee..")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
objCon.Close()

'CREATING TABLE............
Dim conStr1 As String = "Server=.;Database=INDRA;Trusted_Connection = yes"
Dim objCon1 As New SqlConnection(conStr1)
Dim obj1 As SqlCommand
Dim strSQL1 As String
objCon1.Open()
obj1 = objCon1.CreateCommand()
strSQL1 = "CREATE TABLE Indra (ID INT IDENTITY PRIMARY KEY,FirstName VARCHAR(30), LastName VARCHAR (30),Email VARCHAR(30),PhoneNo VARCHAR (15))"
' Execute
obj1.CommandText = strSQL1
Try
obj1.ExecuteNonQuery()
MsgBox("Table Created Successfully...", MsgBoxStyle.Information, "Indranil Chatterjee..")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
objCon1.Close()
objCon1 = Nothing
Me.Hide()
Form2.Show()

Please tell me where i put the above said quieries in the form.
Please help me sir.......

Indranil
RDBurmon 31-Jan-13 10:30am    
I have updated my answer . Try it.
I am online for next an hour.
hspl 1-Feb-13 1:34am    
Sorry to say that after putting the codes in Form_Load event and run it, it will not
create any database in sql server.
RDBurmon 1-Feb-13 1:46am    
Updated my solution . Try now
hspl 1-Feb-13 1:59am    
A message shows that "There is already an open DataReader associated with this Command which must be closed first"
I SOLVED THE PROBLEM AFTER CHANGING THE CODES/LINES SHOWING IN BOLD.....!

'CREATING DATABASE.........................
Dim conStr As String = "Server=.;Database=;Trusted_Connection = yes;MultipleActiveResultSets=True;"
Dim objCon As New SqlConnection(conStr)
Dim obj As SqlCommand
Dim strSQL As String
Dim myReader As SqlDataReader

' Create the database
objCon.Open()
obj = objCon.CreateCommand()

Try
strSQL = "SELECT * FROM sys.databases WHERE name='INDRA'"
obj.CommandText = strSQL
myReader=obj.ExecuteReader()
IF NOT myReader.Read()
myReader.Close()

strSQL = "CREATE DATABASE INDRA"
' Execute
obj.CommandText = strSQL
obj.ExecuteNonQuery()
MsgBox("Database Created Successfully...", MsgBoxStyle.Information, "Indranil Chatterjee..")
END IF
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

objCon.Close()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900