如何在C#代码上添加几个表格表格?

时间:2019-06-26 12:12:51

标签: c# sql-server

创建应用程序,无法弄清楚如何通过单击表单在sql数据库中创建多个表?我有2个连接。 1个用于创建数据库的连接, 2个用于在数据库中创建表的连接

public partial class Form1 : Form
{
    static string constring = ConfigurationManager.ConnectionStrings["Test.Properties.Settings.Setting"].ConnectionString;
    SqlConnection connstringsql = new SqlConnection(constring);

    //for create table in database
    static string create =
ConfigurationManager.ConnectionStrings["Test.Properties.Settings.Setting1"].ConnectionString;
    SqlConnection connscreate = new SqlConnection(create);

    public Form1()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {

        //create table
        connscreate.Open();
        string sqlqueryone = "CREATE TABLE test";
        SqlCommand sqlcomm = new SqlCommand(sqlqueryone, connscreate);
        connscreate.Close();
    }

当我单击底部的按钮以创建多个表时。

2 个答案:

答案 0 :(得分:1)

用代码创建表不是标准的,我也不知道为什么要这么做。
但是您的代码需要改进,我将从为您的所有数据库操作创建一个类开始。
我将编写一个小例子来帮助您

public partial class MyDatabase
{
    private string _connectionString;

    public MyDatabase(string connectionString)
    {
         _connectionString = connectionString;
    }

    public void ExecuteSql(string sqlStatement)
    {
        using (SqlConnection conn = new SqlConnection(_connectionString))
        {
             conn.Open();
             using (SqlCommand command = new SqlCommand(sqlStatement))
             {
                  command.Connection = conn;
                  command.ExecuteNonQuery();
             }
        }
    }
}

,现在您可以根据需要使用它,例如

private void button1_Click(object sender, EventArgs e)
{
    MyDatabase db = new MyDatabase("your connection string");

    db.ExecuteSql("create table test1 (id int, name varchar(10))");
    db.ExecuteSql("create table test2 (id int, name varchar(10))");
    ....
}

这不是一个完整的解决方案,您需要解决其他问题。
例如,在创建表之前,您需要检查该表是否不存在
此示例旨在使您的工作方式比现在更完善

答案 1 :(得分:0)

    private void createDatabase_Click(object sender, EventArgs e)
    {
        string constring = "Data Source =HALLR3C04;User ID=sa;Password=123456;";
        SqlConnection connstringsql = new SqlConnection(constring);

        //create table
        connstringsql.Open();
        string sqlqueryone = "CREATE DATABASE " + textBoxDatabaseName.Text;
        SqlCommand sqlcomm = new SqlCommand(sqlqueryone, connstringsql);
        sqlcomm.ExecuteNonQuery();
        connstringsql.Close();

        listBoxDatabases.Items.Add(textBoxDatabaseName.Text);
    }

    private void createTable_Click(object sender, EventArgs e)
    {
        string constring = $"Data Source =HALLR3C04;User ID=sa;Initial Catalog = {listBoxDatabases.SelectedItem.ToString()};Password=123456;";
        SqlConnection connstringsql = new SqlConnection(constring);

        //create table
        connstringsql.Open();
        string sqlqueryone = $@"create table {textBoxTableName.Text}
                                (
                                    Id int IDENTITY PRIMARY KEY,
                                    Name VARCHAR(50)
                                ) ";
        SqlCommand sqlcomm = new SqlCommand(sqlqueryone, connstringsql);
        sqlcomm.ExecuteNonQuery();
        connstringsql.Close();
    }

enter image description here

enter image description here