使用带有textbox1 textbox2 textbox3的form1创建动态表,需要一些建议

时间:2014-08-08 13:47:52

标签: c# sql database dynamic-programming

您好我是编程新手,我尝试使用Form1创建一个具有textbox1 textbox2textbox3以及CreateButton的应用。

实际上我正在使用Windows窗体创建Database Tables Creator应用程序。 请帮我搞定。

我想知道如何制作它?我收到表创建失败

的错误
    private void CreateButton_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Gaudis\Completed Projects\database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        string qry = "PDynamicTable";
        SqlCommand cmd = new SqlCommand(qry, con);
        try
        {
            SqlParameter p1 = new SqlParameter("@tname", SqlDbType.VarChar, 20);
            SqlParameter p2 = new SqlParameter("@col1", SqlDbType.VarChar, 20);
            SqlParameter p3 = new SqlParameter("@col2", SqlDbType.VarChar, 20);
            p1.Value = textbox1.Text;
            p2.Value = textbox2.Text;
            p3.Value = textbox3.Text;
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            con.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Table Created");
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Table Created failed");
            ex.ToString();

        }
        finally
        {

            con.Close();
        }

    }

1 个答案:

答案 0 :(得分:0)

为自己的问题找到了解决方案,无法按照本网站的t& c删除我的问题。发布此信息以帮助像我一样的新手。 :)

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Login.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
 con.Open();
 SqlCommand cmd = new SqlCommand();
 cmd.Connection = con;
 cmd.CommandType = CommandType.Text;
 cmd.CommandText = "SELECT * FROM INFORMATION_SCHEMA.TABLES";
 SqlDataAdapter dbAdapter = new SqlDataAdapter(cmd);
 DataTable dtRecords = new DataTable();
 dbAdapter.Fill(dtRecords);
 comboBox1.DataSource = dtRecords;
 comboBox1.DisplayMember = "TABLE_NAME";
 con.Close();