How to create a table from C #?

0

I have an application in C # which I made a button, to create a table in my new sqlexpress

I have seen some codes but I have no idea where I should declare them (I imagine that in my "create" button), and the next problem I see is to give my new board a name.

    
asked by chirino 26.01.2018 в 10:39
source

2 answers

0

To create a table in a local database:

        using (var cl = new SqlConnection("Server=(localdb)\MSSqlLocalDb;Database=NOMBREBBDD"))
        {
            cl.Open();
            using (var comm = cl.CreateCommand())
            {
                comm.CommandText = "create table foo(id int primary key, nombre text)";
                comm.ExecuteNonQuery();
            }
        }

I suggest using Entity Framework to access the database in a modern way. With Entity you can manage the creation and update of your data model, and use LINQ to make queries in the database using objects instead of arming queries in SQL.

    
answered by 26.01.2018 в 16:59
0
DataSet ds = new DataSet();
DataTable dt = new DataTable(); //Crea tabla con tres columnas
dt.Columns.Add("1", typeof(int32));
dt.Columns.Add("2", typeof(string));
dt.Columns.Add("3", typeof(string));
dt.Rows.Add(1,"hasan","amiri"); //Agrega contenido a la tabla
ds.Tables.Add(dt); //Agrega la tabla a un DataSet
    
answered by 13.03.2018 в 22:45