Search code examples
c#sqlcreate-table

Create database table using c#.net with variable of table name


I tried so many things but I couldn't find a solution..

private void button1_Click_1(object sender, EventArgs e)
    {

        string NewTable = TxtPanoNo.Text;


        try
        {
            using (SqlCommand command = new SqlCommand("CREATE TABLE" + NewTable + ""+ "(id tinyint,KullanilanAdet smallint,Tip nchar(20),Kod varchar(50),Ad varchar(50),Aciklama varchar(500),Favori bit)", bgl.baglanti()))
            command.ExecuteNonQuery();

        MessageBox.Show("Succeed");
        }

        catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



    }

and the error is:

incorrect syntax near '('.


Solution

  • There is no space between CREATE TABLE and the table-name. Instead use:

    "CREATE TABLE " + NewTable + "(id tinyint, ... and so on