I'm trying to create a command which should create a table based on the name is typed in a textBox. No errors, works, query looks good, but doesn't actually create the table. Why is that?
private void button1_Click(object sender, EventArgs e)
{
int count = 0;
var connString = (@"Data Source=" + System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + @"\Grupe.sdf");
using (var conn = new SqlCeConnection(connString))
{
try
{
conn.Open();
var query = "CREATE TABLE " + textBox1.Text.Trim() + "(" + "Id int NOT NULL IDENTITY (1,1) PRIMARY KEY" ;
MessageBox.Show(query);
foreach (Control c in this.Controls)
{
if (c.Name.Contains("temp") && c is TextBox)
{
if (!String.IsNullOrEmpty(c.Text))
{
query += "," + c.Text.Trim() + " nvarchar(MAX) NULL";
count++;
}
}
}
query += ")";
var command = new SqlCeCommand(query, conn);
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}