Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to know that How can I create Sql Database using value
of a textbox in vb.net application

Indranil
Posted
Comments
Zoltán Zörgő 1-Feb-13 11:00am    
Which of the databases's properties will be the value of the textbox?
Or you want to enter a whole sql script in the textbox and execute - what will stop you from issuing a drop database command?

Hi,

Below code, I have passed the text box value as database name,
VB
Private Sub btnCreateDataBase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDataBase.Click
        Dim conStr As String = "Server=Myserver;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 _" + TextBox1.text
        ' Execute
        obj.CommandText = strSQL
        Try
            obj.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
        objCon.Close()
        objCon = Nothing
End Sub
 
Share this answer
 
v2
Comments
hspl 4-Feb-13 1:45am    
Sir,
I used value of TextBox1.text = 1001
But when i click on CreateDatabase button an error shows that

Incorrect Syntax Near '1001'.
hspl 4-Feb-13 6:58am    
Sir waiting for your reply against my comment....
Muthuraja Irullandi 4-Feb-13 7:12am    
Hi,
You have to specify the database Name in the text box. So that the query will create the database in the SQL Server.
Muthuraja Irullandi 4-Feb-13 7:13am    
Also try with the below connection string
Dim conStr As String = "Server=Myserver;Database=master;Trusted_Connection = yes"
hspl 4-Feb-13 10:03am    
Sir
I want to create database with the numeric value of a textbox
So how can i do that.....
Private Sub btnCreateDataBase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDataBase.Click

DIM dbname AS STRING
dbname = TEXTBOX1.TEXT
Dim conStr As String = "Server=Myserver;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 [" + dbname + "]"
' Execute
obj.CommandText = strSQL
Try
obj.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

objCon.Close()
objCon = Nothing
End Sub

After running this it will successfully create the database having name in numeric.
Indranil
 
Share this answer
 
Comments
A N Saraf 18-Mar-13 2:47am    
The Same Problem Here But with Different Question

Here is My Code

Dim TConnStr As String = "Data Source=(localdb)\v11.0;Integrated Security=true;AttachDbFileName="

Dim TConn As New SqlClient.SqlConnection(TConnStr)

TConn.Open()
Sql = "CREATE DATABASE Test"
Dim Cmd As New SqlClient.SqlCommand(Sql, TConn)
Cmd.ExecuteNonQuery()
TConn.Close()

This Creats a MDF File is Current Users Folder
Now What I want is to create MDF file at desired Location e.g C:\MyDbFiles

What should I Do

Please Help

i'm using SQL SERVER 2012 LocalDB alongwith VB.Net 2012

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



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