如何从asp.net在sqlserver中创建表运行时 [英] how to create table runtime in sqlserver from asp.net

查看:59
本文介绍了如何从asp.net在sqlserver中创建表运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gridview中,我显示字段名称,数据类型和长度..单击按钮后,我要在sqlserver中创建表.

In gridview i display field name, datatype and length.. on button click i want to create table in sqlserver.

Filed_Name   Datatype  Length<br />
Studname    varchar    100<br />
Add         varchar     200<br />
age         int         2



这是我的gridview的结构.如何从这种结构在sqlserver中创建表.



this is structure of my gridview.. how to create table in sqlserver from this structure.

推荐答案

使用创建表"命令:http://msdn.microsoft.com/en-us/library/ms174979.aspx [
use the Create Table command: http://msdn.microsoft.com/en-us/library/ms174979.aspx[^]

so you create string with the create table command like:
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("Create Table {0}", DatabaseTableNameValue));
foreach (GridView row in MyGrid)
{
if (row.RowType == DataControlRowType.DataRow)
    sv.AppendLine(string.Format("{0} (1} {2}", Field_name, DataType, Lenght));
}


//现在您有了可以通过带有SqlConnection的SqlCommand类发送到数据库的查询


//now you have the query you can send via SqlCommand class with an SqlConnection to your DB


在按钮单击事件中使用以下过程

1.使用逗号分隔的字符串来表示字段名称,数据类型和长度.

fieldname = field1,field2,field3等
数据类型=数据类型1,数据类型2,数据类型3等
长度= length1,length2,length3等

2.使用4个参数@ str_fieldname,@ str_datatype,@ str_Length,@ TableName
在sql中创建SP 和

3.从asp.net页上调用此sp,然后将变量值发送到这些参数.

4.在SP中,使用拆分功能使用逗号拆分所有这些变量

5.编写创建表语法并添加动态参数值.

Hi, use below process at button click event

1. make comma separated string for fieldname, datatype and length.
i.e.
fieldname = field1,field2,field3 and so on
datatype = datatype1,datatype2,datatype3 and so on
length = length1,length2,length3 and so on

2. Make a SP in sql with 4 parameters @str_fieldname, @str_datatype, @str_Length, @TableName
and

3. Call this sp from asp.net page and send variable values to these parameter.

4. In SP, split all these variable with comma using split function

5. write create table syntax and add dynamic parameter values.

Set @query = ''Create Table ''+@TableName+'' (''+@str+'')''



@str是您使用fieldname数据类型(长度)等创建的字符串.

使用上述过程,如果使用后遇到任何问题,请告诉我.



Here @str is string which you made with using fieldname datatype (length) and so on.

Use above process, if you have any problem after using this then tell me.


请参阅此以获取SP的基础知识
http://www.informit.com/articles/article.aspx?p=25288& seqNum = 4 [ ^ ]
Refer this for basics of SP
http://www.informit.com/articles/article.aspx?p=25288&seqNum=4[^]


这篇关于如何从asp.net在sqlserver中创建表运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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