Hi
I developed a program. This program will use a SQL server database. How can I create the database using code. I think I need to use sql scipt like this:
create database customers
But where should I write the script and How can I make VB code implement the script.
THanks a lot
You can just execute the sql statement.
e.g.
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "create database [db1]";
cmd.CommandType = CommandType.Text;
//execute
cmd.ExecuteNonQuery();
}
Thank you very much.
I used the following code. It worked in the first time. But when I debugged the program for the second time, I had this error " database db1 already exists"
How can I solve this problem?
And can you give me a sql script to create one database with one three-column table?
thanks
Using (cmdGet)
cmdGet.Connection = conGet
cmdGet.CommandText = "create database [db1]"
cmdGet.CommandType = CommandType.Text
cmdGet.ExecuteNonQuery()
End Using
|||
Code Snippet
'create db
cmdGet.CommandText = "if db_id('db1') is null exec('create database [db1]')"
cmdGet.CommandType = CommandType.Text
cmdGet.ExecuteNonQuery()
'create table
cmdGet.CommandText = "use db1; if object_id('tb1') is null exec('create table tb1(col1 int, col2 int, col3 int)')"
cmdGet.ExecuteNonQuery()
No comments:
Post a Comment