Answer the question
In order to leave comments, you need to log in
Sql CreateTable runs indefinitely?
I created a local database through the Context menu - Add - a database based on services,
I wrote a small class where I copied the connection string from the object:
public static class Database
{
public static SqlConnection Connection { set; get; }
public static void CreateConnection()
{
var connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Data\MyDb.mdf;Integrated Security=True";
Connection = new SqlConnection(connectionString);
}
}
public static void CreateTable(string fileName, string[] fields)
{
var tableFields = string.Join(Environment.NewLine, fields.Select(x => $"[{x}] NVARCHAR(250) NULL,"));
var sql = [email protected]"CREATE TABLE [dbo].[{fileName}] (
[Id] INT IDENTITY (1, 1) NOT NULL,
{tableFields}
PRIMARY KEY CLUSTERED ([Id] ASC)
);";
Connection.Open();
var com = new SqlCommand(sql, Connection);
int result = com.ExecuteNonQuery();
Connection.Close();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question