如何动态地使用asp.net在sql中创建表? [英] how to create Table in sql by using asp.net dynamically?
问题描述
HI,
我使用asp.net在sqlserver中创建一个表。这是我的代码在执行这个时遇到问题创建表请帮我解决这个问题....
我的。 aspx代码是
iam creating a table in sqlserver by using asp.net. here is my code while executing this i get a problem to create table please help me to solve this....
my .aspx code is
<pre lang="HTML"><pre lang="HTML"><pre lang="HTML">
<table>
<tr>
<td> Enter Table Name </td>
<td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Create" onclick="Button1_Click" />
</td>
</tr>
</table>
和我的.aspx.cs页面是......
string command =(创建表+ TextBox1.Text);
SqlConnection c on = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand(command,con);
int i = cmd.ExecuteNonQuery();
con.Close();
这里我得到了SqlException,错误信息是incorect syntax在krishna附近。
这里krishna是我在文本框中给出的表名。
and my .aspx.cs page is...
string command= ("Create Table "+TextBox1.Text);
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand(command, con);
int i= cmd.ExecuteNonQuery();
con.Close();
Here i got SqlException and the error message is " incorect syntax near krishna ".
here krishna is table name which i given in the textbox.
推荐答案
你应该在创建中提及列名及其名称表声明。
语法是
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
因此请相应地修改您的查询并尝试。
谢谢。
You should mention column names and their in Create Table statement.
The syntax is
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
So modify your query accordingly and try.
Thanks.
这篇关于如何动态地使用asp.net在sql中创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!