动态创建数据库中的表并插入数据 [英] dynamically create table in database and insert data

查看:127
本文介绍了动态创建数据库中的表并插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个包含numericupdown控件和一个按钮的表单,在运行中我给出一个数字并单击一个按钮。那么它将打开form2,在那里它将创建文本框,我在数字下拉控件中给出的数字。







我的问题是基于我必须创建一个表和列的数字,插入后我在textbox.i中给出的数据有以下类



1.form1

  private   void  button1_Click ( object  sender,EventArgs e)
{
Form2 f = new Form2( );
global .count = Convert.ToInt32(numericUpDown1.Value.ToString());
f.Show();
}





2.form2

private void Form2_Load(object sender,EventArgs e)

{

for(int i = 0; i< global.count;> {

TextBox tb = new TextBox();

tb.Name =TextBOX+ i;

tb.Size = new Size(100,25);

tb.Location = new点(25,25 +(i * 30));

Controls.Add(tb);



}



}

DAL dal = new DAL(); // dal class

string create =create table MYTABLE() ; //如何动态创建列

解决方案

使用下面的代码动态生成查询并使用您的方法在数据库上执行它:

< pre lang =c#> string create = 创建表MYTABLE(;
for int i = 1 ; i < = count; i ++)
{
create + = column + i.ToString()+ INT;

if (i!= count)
create + = ;
}
create + = ;


构建插入查询,如下所示:

  string  insert =   INSERT INTO MYTABLE(; 
string values = string .Empty;

for (< span class =code-keyword> int i = 1 ; i < = count; i ++)
{
insert + = column + i。 ToString();
TextBox txtbox =(TextBox) this .Controls.Find( < span class =code-string> TextBOX + i.ToString(), true )[ 0 ]; // 设置文本框ID以根据您的表单进行搜索
values + = ' + txtbox.Text.Trim()+ '; // 如果数据类型为整数,则省略单引号,即值+ = txtbox.Text.Trim();

if (i!= count)
{
insert + = ;
values + = ;
}
}
insert + = )VALUES( + values + ;

// 在此处插入代码


hi,

i have a form which contains numericupdown control and a button,in running i give a number and click a button. then it will open form2 ,there it will create textboxes the number i give in numericupdown control.



my question is based on the number i have to create a table and columns and after insert the data which i have given in the textbox.i have the following classes

1.form1

private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            global.count = Convert.ToInt32(numericUpDown1.Value.ToString());
            f.Show();
        }



2.form2
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i <global.count;> {
TextBox tb = new TextBox();
tb.Name = "TextBOX" + i;
tb.Size = new Size(100, 25);
tb.Location = new Point(25, 25 + (i * 30));
Controls.Add(tb);

}

}
DAL dal = new DAL();//dal class
string create = "create table MYTABLE()";//how to create columns dynamically

解决方案

Use below code to generate query dynamically and execute it on database using your method:

string create = "CREATE TABLE MYTABLE (";
for (int i = 1; i <= count; i++)
{
    create += "column" + i.ToString() + " INT";

    if (i != count)
        create += ",";
}
create += ")";


Build insert query as below:

string insert = "INSERT INTO MYTABLE (";
string values = string.Empty;

for (int i = 1; i <= count; i++)
{
    insert += "column" + i.ToString();
    TextBox txtbox = (TextBox)this.Controls.Find("TextBOX" + i.ToString(), true)[0];    //set textbox id to search as per your form
    values += "'" + txtbox.Text.Trim() + "'";   //omit single quotes if datatype is integer i.e. values += txtbox.Text.Trim();

    if (i != count)
    {
        insert += ",";
        values += ",";
    }
}
insert += ") VALUES (" + values + ")";

//insert code here


这篇关于动态创建数据库中的表并插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆