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();

解决方案 »

  1.   

    表名师不可以用 @NewUserTableName  这种方式的变量的
      

  2.   

    改成这样string CreatTableString="Create Table “+NewUserTableName+
    "([id][int] NOT NULL primary key,"+
    " [fmobile] [varchar](21) NOT NULL,"+
    " [regtime] [DateTime] NULL,"+
    "[jobid] [int] NULL)"+
    "Go";
      

  3.   

    private void CreateTableBtn_Click(object sender, System.EventArgs e)
            {             // 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());
                }
            }
      
      

  4.   

    那就是应该你的 sql 语句有问题  你直接到sqlserver 看看能不能成功
      

  5.   

    那表名不能用变量了吗
     表明当然能够用变量了
    你是对这个CreatTableString字符串是用变量
    生成的SQL 语句没有变两
      

  6.   


    在SQLSERVER能运行成功(指定表名)那我应该怎么把CreatTableString的值作为新表的名称啊?
      

  7.   

    create procedure up_temp asdeclare @a varchar(400), @b varchar(400)
    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';注意双引号。
      

  8.   

    我给你写一下 在VB.NET 中如何实现的你自己在该为C#DIM creatTableString as string 
    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";
      

  9.   

    我明白你说的意思,这样写就是没通过(C#+asp.net  Web服务)
    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();

    }
      

  10.   

    我測試過沒問題的,你看看
    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();