int count;
string ConnectionString = "server = (local) ; database = Test ; uid = sa ; pwd = xiaotuni";
SqlConnection cn = new SqlConnection(ConnectionString);
cn.Open();
string CreateTableString = this.txtCreateTableString.Text;
SqlCommand cmd = new SqlCommand(CreateTableString, cn);
count = cmd.ExecuteNonQuery();
~~~~~ 这个不是用来返回所受影响的行数,可是它返回一个 -1 不知道为什么.
Response.Write(count);
cn.Close();以上是创建表的代码
但是每次返回都是一个 -1 不知道为什么
但是在数据库里查看一下吧
又创建了此表.
解决方案 »
- dropdownList的问题
- 为什么我对tr设置visible="false"不起作用
- 获取使用的内存大小
- fpspread打印有人用过吗?
- vb写的ocx控件在asp.net中如何使用?
- 复合表头的repeater导出excel的问题
- <authentication mode="Forms"> 关于验证的一个问题,帮忙啊?
- 有点挑战性的问题,大家一起讨论一下
- 求代码!以某列不重复的值为条件获取数据【asp.net,sqlserver】,在线等,可以运行就结贴
- 上传文件的字节最大限制是多少啊?
- Rio~~ 组单选控件,取消选择,还原到原选择的实现??
- 用数据库驱动RadioButtonList时怎么让其自动选一个Radio?
返回值为该命令所影响的行数。对于其他所有类型的语句,返回值都为 -1所以不能根据返回值判断是否成功创建了表,而应该用try-catch语句尝试捕获异常
SqlConnection cn = new SqlConnection(ConnectionString);
try
{
cn.Open();
string CreateTableString = this.txtCreateTableString.Text;
SqlCommand cmd = new SqlCommand(CreateTableString, cn);
cmd.ExecuteNonQuery();
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if(cn.State == ConnectionState.Open)
cn.Close();
}