如何通过用户界面点击按钮在数据库中创建sql表 [英] How can i create sql table in database through button click with user interface

查看:137
本文介绍了如何通过用户界面点击按钮在数据库中创建sql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击按钮创建Sql表......

我使用这个命令: -



 con.Open(); 

SqlCommand cmd = new SqlCommand( create table @table(@ col1 @ datatype1,@ col2 @ datatype2,@ col3 @ datatype3,@ col4 @ datatype4,@ col5 @ datatype5),con);

cmd.Parameters.Add( new SqlParameter( @表,textBox6.Text));
cmd.Parameters.Add( new SqlParameter( @ col1,textBox1.Text.ToUpper()));
cmd.Parameters.Add( new SqlParameter( @ col2,textBox2.Text.ToUpper()));
cmd.Parameters.Add( new SqlParameter( @ col3,textBox3.Text.ToUpper()));
cmd.Parameters.Add( new SqlParameter( @ col4,textBox4.Text.ToUpper()));
cmd.Parameters.Add( new SqlParameter( @ col5,textBox5.Text.ToUpper()));

cmd.Parameters.Add( new SqlParameter( @ dataType1,comboBox1.SelectedItem.ToString()));

cmd.Parameters.Add( new SqlParameter( @ dataType2,comboBox3.SelectedItem.ToString()));

cmd.Parameters.Add( new SqlParameter( @ dataType3,comboBox5.SelectedItem.ToString()));

cmd.Parameters.Add( new SqlParameter( @ dataType4,comboBox7.SelectedItem.ToString()));

cmd.Parameters.Add( new SqlParameter( @ dataType5,comboBox9.SelectedItem.ToString()));

cmd.ExecuteNonQuery();
con.Close();





请检查此编码............. ....

解决方案

请先阅读我对这个问题的评论。



如果你想要要执行SQL命令来创建表,您需要授予权限 [ ^ ]给当前用户。



就个人而言,我不建议允许用户创建未知数量的表。谁能管理它们?





你不能创建查询字符串以这种方式:

  create   table   @ table  @ col1   @ datatype1  @ col2   @ datatype2  @ col3   @ datatype3  @ col4   @ datatype4  @ col5   @ datatype5  



以上代码永远不会编译!



你可以创建动态SQL [ ^ ]代码,传递到 EXEC或EXECUTE [ ^ ]方法。



  DECLARE   @sql   VARCHAR  255 ) = '  SELECT * FROM TableName' 
EXEC @ sql





如需了解更多信息,请请参阅:

在SQL中编写安全动态SQL服务器 [ ^ ]

在存储过程中构建动态SQL [ ^ ]

两种便利技巧用于在存储过程中创建动态SQL [ ^ ]

了解SQL Server如何执行查询 [ ^ ]

如何使用动态SQL生成T-SQL代码? [ ^ ]



记住,你已经收到警告了!




不要这样做。



尝试创建一个可以执行此操作的存储过程。在你的sql中创建下面的存储过程,并在proc中执行相同的所有验证。



 创建  proc  createTable 

@ tableName varchar 30 ),
@ column1 varchar 30 ),
@ column1DataType varchar 30 ),
@ column2 varchar 30 ),
< span class =code-sdkkeyword> @ column2DataType varchar 30

AS
DE CLARE @ CreateTableString NVARCHAR (MAX)
BEGIN
IF EXISTS SELECT 名称 FROM sys.tables WHERE name = @ tableName)
BEGIN
PRINT ' 表ALREADY EXISTS');
RETURN -1;
END
ELSE
BEGIN
SET @ CreateTableString = ' CREATE TABLE' + @ tableName + ' (' + @ column1 + < span class =code-string>' ' + @ column1DataType + ' ,' + @ column2 + ' ' + @ column2DataType + < span class =code-string>' )'
EXEC @ CreateTableString );
PRINT ' TABLE CREATED SUCCESSFULLY'< /跨度>);
END
END





现在从前端开始,你必须创建所有值为字符串的sql参数然后执行它。应该传递的方式如下: -



  EXEC  createTable '  test1''  test1c1''  varchar(30)',< span class =code-string>'  test1c2''  varchar(30)' 





我想这很好地解决了这个问题。我试过在我的系统中运行它很好。



投票并将其标记为解决方案,如果它解决了你的问题..

问候

Jashobanta


如果要以这种方式创建表,则可能会出现错误,因为创建表需要对数据库具有某些特定权限。但有一种方法可以做到这一点。您可以使用Entity Framework Code第一种方法。您可以在程序中创建类,实体框架将在运行时创建数据库中的所有表和关系映射。



通过以下链接,你可以做很容易我猜...不是一件很难的事情......



http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx [ ^ ]



http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework -4.aspx [ ^ ]



投票并将其标记为解决方案,如果它解决了您的问题..

问候

Jashobanta


I want to create Sql table through the click of button......
I use this command:-

con.Open();

SqlCommand cmd = new SqlCommand("create table @table (@col1 @datatype1, @col2 @datatype2, @col3 @datatype3, @col4 @datatype4, @col5 @datatype5 )", con);

cmd.Parameters.Add(new SqlParameter("@table",textBox6.Text)); 
cmd.Parameters.Add(new SqlParameter("@col1", textBox1.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col2", textBox2.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col3", textBox3.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col4", textBox4.Text.ToUpper()));
cmd.Parameters.Add(new SqlParameter("@col5", textBox5.Text.ToUpper()));

cmd.Parameters.Add(new SqlParameter("@dataType1", comboBox1.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType2", comboBox3.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType3", comboBox5.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType4", comboBox7.SelectedItem.ToString()));

cmd.Parameters.Add(new SqlParameter("@dataType5", comboBox9.SelectedItem.ToString()));

cmd.ExecuteNonQuery();
con.Close();



Please Check this coding.................

解决方案

Please, read my comment to the question first.

If you want to execute SQL command to create table, you need to grant PERMISSIONS[^] to the current user.

Personally, i would not recommend to allow users to create unknown number of tables. Who will be able to manage them?

[EDIT]
You can't create query string in that way:

create table @table (@col1 @datatype1, @col2 @datatype2, @col3 @datatype3, @col4 @datatype4, @col5 @datatype5


Above code will never compiled!

You can create dynamic SQL[^] code, which is passed into EXEC or EXECUTE[^] method.

DECLARE @sql VARCHAR(255) = 'SELECT * FROM TableName'
EXEC(@sql)



For further information, please see:
Writing Secure Dynamic SQL in SQL Server[^]
Building Dynamic SQL In a Stored Procedure[^]
Two Handy Techniques for Creating Dynamic SQL in Stored Procedures[^]
Understanding how SQL Server executes a query[^]
How to Generate T-SQL Code by Using Dynamic SQL?[^]

Remember, you've been warned!

[EDIT]


DO NOT DO IT IN THIS WAY.

Try creating a stored proc that can do this. Create the below stored procedure in your sql and do all the validations from your end on the same in the proc.

create proc createTable
(
@tableName varchar(30),
@column1 varchar(30),
@column1DataType varchar(30),
@column2 varchar(30),
@column2DataType varchar(30)
)
AS
DECLARE @CreateTableString NVARCHAR(MAX)
BEGIN
	IF EXISTS (SELECT NAME FROM sys.tables WHERE name=@tableName)
		BEGIN	
			PRINT ('TABLE ALREADY EXISTS');
                        RETURN -1;
		END
	ELSE
		BEGIN
			SET @CreateTableString='CREATE TABLE '+@tableName +'('+@column1+' ' +@column1DataType+','+@column2+' ' +@column2DataType+')'
			EXEC (@CreateTableString);
                        PRINT ('TABLE CREATED SUCCESSFULLY');
		END
END



Now from your front end, you have to create sql parameters with all values as strings and then execute it. They way it should be passed is like :-

EXEC createTable 'test1','test1c1','varchar(30)','test1c2','varchar(30)'



I guess this solves the issue well. I have tried running it in my system and it is fine.

Vote it and mark it as solution if it solves your problem..
Regards
Jashobanta


If you want to create a table in such a manner, you might be getting errors as creating table needs some specific permissions on the database. But there is a way to do this. You can use Entity Framework Code first approach for this. You can create classes in your program and entity framework will be creating all the tables and relational mappings in your database at runtime.

Go through the below links and you can do it easily I guess.. not a tough thing to do..

http://www.hanselman.com/blog/SimpleCodeFirstWithEntityFramework4MagicUnicornFeatureCTP4.aspx[^]

http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx[^]

Vote it and mark it as solution if it solves your problem..
Regards
Jashobanta


这篇关于如何通过用户界面点击按钮在数据库中创建sql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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