jeevan mummadi

jeevan mummadi

  • NA
  • 157
  • 0

dynamically create sql database & table using c#.net(errors)

Jun 6 2008 2:59 AM

i create sql database and table at runtime but i have some errors there are given below

this is for create bd:-

string ConnectionString ="server=localhost;user id=sa;pwd=";

SqlConnection conn = new SqlConnection(ConnectionString);

conn.Open();

string sql = "CREATE DATABASE mydb ON PRIMARY"
+"(Name=test_data, filename = 'C:\\mysql\\mydb_data.mdf', size=3,"
+"maxsize=5, filegrowth=10%)log on"
+"(name=mydbb_log, filename='C:\\mysql\\mydb_log.ldf',size=3,"
+"maxsize=20,filegrowth=1)" ;

SqlCommand cmd = new SqlCommand(sql, conn);

try

{

cmd.ExecuteNonQuery();

}

catch (SqlException ex)

{

MessageBox.Show(ex.Message.ToString());

}

conn.Close();

above create db code i got error invalid name ,i want to create db and mdf and ldf files

plz verift the sql string and post me correct code

 

 

this is for create table:

----------------------------

SqlConnection con = new SqlConnection("server=localhost;user id=sa;pwd=");

con.Open();

string st = "CREATE TABLE"+ textBox1.Text;

//"(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," +

//"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)";

SqlCommand cmd = new SqlCommand(st,con);

try

{

cmd.ExecuteNonQuery();

}

catch(SqlException ex)

{

MessageBox.Show(ex.Message.ToString());

}

con.Close();

 

in create table code i want create table given in textbox(mytable),when click create table button

the table not created i got invalid table name error

plz help me...

 


Answers (2)