查看: 1426|回复: 11
|
[求救]如何用vb.net来create new database table?
[复制链接]
|
|
请问各位,如何用vb.net来create new database table?
在我的program里,将会用到很多的database table,而这些database table都是由user来create的。
请问有谁有这样的source code吗?
谢谢!! |
|
|
|
|
|
|
|

楼主 |
发表于 19-2-2006 11:32 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 20-2-2006 11:45 PM
|
显示全部楼层
Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
Dim cmdStr As String = "create table myTable" + "(pStoryId integer constraint pKeypStoryId primary key," + "stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)"
Dim cmd As New SqlCommand(cmdStr, myConnection)
myConnection.Open()
cmd.ExecuteNonQuery()
myConnection.Close()
如果用以上的方法,我的table name一定是myTable而已。
我想请问有哪一位知道如何把它改为那个table name是可以给user key in的?
例如我有一个textbox1,我想让user来key in他所想要放的名字,然后我再读他。
Dim cmdStr As String = "create table '" + textbox1.Text + "'" + "(pStoryId integer constraint pKeypStoryId primary key," + "stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)"
以上的code对吗?
我run不到,system说ExecuteNonQuery()做不到。
请各位帮帮忙。。。
谢谢!!
[ 本帖最后由 寻找自由的天空 于 20-2-2006 11:46 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 21-2-2006 09:11 AM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 21-2-2006 11:56 AM
|
显示全部楼层
原帖由 goatstudio 于 21-2-2006 09:11 AM 发表
应该没问题的, 你的完整错误讯息是什么?
行不通。
错误讯息是[SqlException: Line 1: Incorrect syntax near 'ABC'.]
ABC是我放在textbox内的。
然后它还把cmd.ExecuteNonQuery()highlight起来。
这是怎么回事?
谢谢。 |
|
|
|
|
|
|
|
发表于 21-2-2006 12:34 PM
|
显示全部楼层
行得通, 只是你的 sql 错误而已.
建议你把 cmdStr 这 variable 打印出来看看你的 sql 错在那里. |
|
|
|
|
|
|
|

楼主 |
发表于 21-2-2006 02:42 PM
|
显示全部楼层
原帖由 goatstudio 于 21-2-2006 12:34 PM 发表
行得通, 只是你的 sql 错误而已.
建议你把 cmdStr 这 variable 打印出来看看你的 sql 错在那里.
还是行不通。
Dim cmdStr As String = "create table myTable (pStoryId integer constraint pKeypStoryId primary key, stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)" (这是没问题的,用SQL Query Analyzer test了,也没有问题)
但是当我把它改成
Dim cmdStr As String = "create table '"+txtName.Text+"' (pStoryId integer constraint pKeypStoryId primary key, stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)"
或者
Dim cmdStr As String = "create table '"+Request.Form("txtName").ToString+"' (pStoryId integer constraint pKeypStoryId primary key, stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)"
就行不通了,出现相同的error。
这到底是怎么一回事?
谢谢。
[ 本帖最后由 寻找自由的天空 于 21-2-2006 02:47 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 21-2-2006 02:47 PM
|
显示全部楼层
试试把单引号去掉
Dim cmdStr As String = "create table "+txtName.Text+" (pStoryId integer constraint pKeypStoryId primary key, stuId varchar(50), pTitle varchar(50), pContent varchar(500), pImage image)" |
|
|
|
|
|
|
|

楼主 |
发表于 21-2-2006 03:00 PM
|
显示全部楼层
原帖由 sson 于 21-2-2006 02:47 PM 发表
试试把单引号去掉
Dim cmdStr As String = "create table "+txtName.Text+" (pStoryId integ ...
谢谢。
这就可以解决了。
但是,又有新的问题了,当我create了第一个我要的table之后,system就不给我create另一个table了。
error message:There is already an object named 'pKeypStoryId' in the database. Could not create constraint. See previous errors.
但是如果我自己本身在database那儿,create两个table,用相同的attribute就没有问题。
这到底又是怎么一回事?
谢谢。 |
|
|
|
|
|
|
|

楼主 |
发表于 26-2-2006 06:21 PM
|
显示全部楼层
没有人可以帮忙吗?
还是有什么解决方案吗?
先谢谢啦。 |
|
|
|
|
|
|
|
发表于 26-2-2006 06:54 PM
|
显示全部楼层
呵呵,那是当然的咯
因为 create table 这个指令是没有引号的。
新的问题小章鱼不懂(小章鱼也不是很会 SQL)
不过想请问 constraint 这个指令后面跟随着的不是 primary key/not null 吗?
你的 pKeypStoryId 是什么意思? |
|
|
|
|
|
|
|

楼主 |
发表于 2-3-2006 04:19 PM
|
显示全部楼层
原帖由 sson 于 26-2-2006 06:54 PM 发表
呵呵,那是当然的咯
因为 create table 这个指令是没有引号的。
新的问题小章鱼不懂(小章鱼也不是很会 SQL ...
其实我也不是很明白,我只是拿人家的coding来做reference然后自己改一改。
我已经找到解决方案了。
谢谢所有帮助过我的人。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|