Question : ASP.NET, C# Create table with a variable name for Table Name

hi experts,

I am creating a table from C#, asp.net .This is my code:

            DbOpen();
            string strStatement = "CREATE TABLE MyNewName(PersonID COUNTER PRIMARY KEY, FirstName varchar(20), LastName varchar(20))";
            OleDbCommand DBCommand = new OleDbCommand(strStatement, DBConnection);
            DBCommand.ExecuteNonQuery();
            DbClose();

It is creating a table in my database. No problem. But, I would like MyNewName (for the table name) to be a variable - string.

I tried this way:
            string strStatement = "CREATE TABLE @TblName(PersonID COUNTER PRIMARY KEY, FirstName varchar(20), LastName varchar(20))";

DBCommand.Parameters.Add("@TblName", OleDbType.VarChar, 100).Value = "MyNewTable";

Did not work.

I am also wondering how then Insert statement works?

I am thinking something like this.. but does not work... lol

            DbOpen();
            string Query = "INSERT INTO @TblName(FirstName, LastName) VALUES(@FirstName, @LastName)";
            OleDbCommand DBCommand1 = new OleDbCommand(Query, DBConnection);
            DBCommand1.Parameters.Add("@TblName", OleDbType.VarChar, 100).Value = "MyNewTable";
            DBCommand1.Parameters.Add("@FisrtName", OleDbType.VarChar, 100).Value = "DFDR";
            DBCommand1.Parameters.Add("@LastName", OleDbType.DBDate, 10).Value = "CXFD";
            DBCommand1.ExecuteNonQuery();
            DbClose();


Answer : ASP.NET, C# Create table with a variable name for Table Name

try this...

string strStatement = "CREATE TABLE " + strTableName + "(PersonID COUNTER PRIMARY KEY, FirstName varchar(20), LastName varchar(20))"
Random Solutions  
 
programming4us programming4us