Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I want to know that How can I create Sql Database using value
of a textbox in vb.net application
 
Indranil
Posted 1-Feb-13 5:57am
hspl239
Comments
Zoltán Zörgő at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Below code, I have passed the text box value as database name,
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
  Permalink  
v2
Comments
hspl at 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 at 4-Feb-13 6:58am
   
Sir waiting for your reply against my comment....
Muthuraja Irullandi at 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 at 4-Feb-13 7:13am
   
Also try with the below connection string
Dim conStr As String = "Server=Myserver;Database=master;Trusted_Connection = yes"
hspl at 4-Feb-13 10:03am
   
Sir
I want to create database with the numeric value of a textbox
So how can i do that.....
Muthuraja Irullandi at 4-Feb-13 10:33am
   
Hi,
As per the database naming rules you cannot give the numeric as database name . But you can give the name as below.
 
CREATE DATABASE _1234
 
Please see the modified solution
hspl at 5-Feb-13 1:16am
   
Dear Sir,
I am sorry to say that as per database naming rules we cannot give the numeric as database name which you have told in your last comment.
Well i feel happy that i solved the problem with my own tricks and able to create
NUMERIC as database name, below is the my solution......
 
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Amit N. Saraf at 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)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100