C# - Create SQL Server table programmatically
You haven't mentioned the Initial catalog
name in the connection string. Give your database name as Initial Catalog
name.
<add name ="AutoRepairSqlProvider" connectionString=
"Data Source=.\SQLEXPRESS; Initial Catalog=MyDatabase; AttachDbFilename=|DataDirectory|\AutoRepairDatabase.mdf;
Integrated Security=True;User Instance=True"/>
First, check whether the table exists or not. Accordingly, create table if doesn't exist.
var commandStr= "If not exists (select name from sysobjects where name = 'Customer') CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime)";
using (SqlCommand command = new SqlCommand(commandStr, con))
command.ExecuteNonQuery();
For managing DataBase Objects in SQL Server i would suggest using Server Management Objects