this.Open_DataBase();
smsCommand=new SqlCommand(CreatTableString,smsConn);
smsCommand.Parameters.Add("@NewUserTableName",SqlDbType.VarChar,20);
smsCommand.Parameters["@NewUserTableName"].Value=NewUserTableName;
smsCommand.ExecuteNonQuery();
this.Close_DataBase();
smsCommand.Dispose();
smsCommand=new SqlCommand(CreatTableString,smsConn);
smsCommand.Parameters.Add("@NewUserTableName",SqlDbType.VarChar,20);
smsCommand.Parameters["@NewUserTableName"].Value=NewUserTableName;
smsCommand.ExecuteNonQuery();
this.Close_DataBase();
smsCommand.Dispose();
"([id][int] NOT NULL primary key,"+
" [fmobile] [varchar](21) NOT NULL,"+
" [regtime] [DateTime] NULL,"+
"[jobid] [int] NULL)"+
"Go";
{ // Open the connection
if( conn.State == ConnectionState.Open)
conn.Close(); ConnectionString ="Integrated Security=SSPI;" +
"Initial Catalog=mydb;" +
"Data Source=localhost;"; conn.ConnectionString = ConnectionString;
conn.Open(); sql = "CREATE TABLE myTable"+
"(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY,"+
"myName CHAR(50), myAddress CHAR(255), myBalance FLOAT)" ; cmd = new SqlCommand(sql, conn);
try
{
cmd.ExecuteNonQuery();
// Adding records the table
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) "+
"VALUES (1001, 'Puneet Nehra', 'A 449 Sect 19, DELHI', 23.98 ) " ;
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) "+
"VALUES (1002, 'Anoop Singh', 'Lodi Road, DELHI', 353.64) " ;
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) "+
"VALUES (1003, 'Rakesh M', 'Nag Chowk, Jabalpur M.P.', 43.43) " ;
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); sql = "INSERT INTO myTable(myId, myName, myAddress, myBalance) "+
"VALUES (1004, 'Madan Kesh', '4th Street, Lane 3, DELHI', 23.00) " ;
cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery(); }
catch(SqlException ae)
{
MessageBox.Show(ae.Message.ToString());
}
}
表明当然能够用变量了
你是对这个CreatTableString字符串是用变量
生成的SQL 语句没有变两
在SQLSERVER能运行成功(指定表名)那我应该怎么把CreatTableString的值作为新表的名称啊?
set @b = 'tablename'set @a = 'Create Table ' + @b + '(id int not null, fmobile varchar(20) not null, regtime datetime null, jobid int null, primary key(id))'exec( @a )
GO上面的存储过程已经调通了。所以:string CreatTableString='Create Table ' + NewUserTableName + '(id int not null, fmobile varchar(20) not null, regtime datetime null, jobid int null, primary key(id)) go';注意双引号。
dim NewUserTableName as string
NewUserTableName="youTablename"
CreatTableString="Create Table “+NewUserTableName+
"([id][int] NOT NULL primary key,"+
" [fmobile] [varchar](21) NOT NULL,"+
" [regtime] [DateTime] NULL,"+
"[jobid] [int] NULL)"+
"Go";
public void CreatNewTable(string NewUserTableName)
{ string CreatTableString="Create Table "+NewUserTableName+
"([id][int] NOT NULL primary key,"+
" [fmobile] [varchar](21) NOT NULL,"+
" [regtime] [DateTime] NULL,"+
"[jobid] [int] NULL)"+
"Go";
this.Open_DataBase();
smsCommand=new SqlCommand(CreatTableString,smsConn);
smsCommand.ExecuteNonQuery();
this.Close_DataBase();
smsCommand.Dispose();
}
Conn.Open();
string sql;
sql="create table "+Text1.Text+" (id int,i int,s varchar(20)) ";
SqlCommand comm=new SqlCommand(sql,Conn);
comm.ExecuteNonQuery();
Conn.Close();