C# SqlDataAdapter执行Sql出错

Kamiliy 2016-08-16 01:41:49
如题,我在执行一段sql代码的时候报错,错误如下:


但是我的Sql语句在Sqlserver工具里面执行是没有报任何问题的。

if exists (select 1 from sysobjects where name = 'sysproperties'and xtype = 'V')
begin
DROP VIEW sysproperties;
end
go
CREATE VIEW sysproperties AS SELECT class AS id,Minor_id AS sMallid,* from sys.extended_properties;
go
Select
表名= d.name ,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then 'identity(1,1)' else '' end,
主键=case when exists(Select 1 FROM sysobjects where xtype='PK' and name in
(Select name FROM sysindexes Where indid in(Select indid FROM sysindexkeys Where id=a.id AND colid=a.colid))) then 'primary key' else '' end,
类型=b.name,
字段长度=a.length,
占用字节数=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then 'null'else 'not null' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xusertype=b.xusertype
inner join sysobjects d on (a.id=d.id)and(d.xtype='U')and(d.name<>'dtproperties')
left join syscomments e on a.cdefault=e.id
left join sysproperties g on (a.id=g.id)and(a.colid=g.smallid)
left join sysproperties f on (d.id=f.id)and(f.smallid=0)
where d.name='@tablename' --如果只查询指定表,加上此条件
order by a.id,a.colorder ;

sql查询时CommandType=Text 不知道是什么原因,有大神告知一下吗?是因为那个判断的问题?如果是请大神指导下,这句话我应该怎么修改呢? 我需要的是能直接执行的sql(),视图和存储过程都不用推荐,谢谢。
...全文
279 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoqiu_net 2016-08-16
  • 打赏
  • 举报
回复
异常提示的很明确了,怎么不仔细找找了。。。
  • 打赏
  • 举报
回复
将go删掉,程序里访问sql不像在查询分析器里那样,你用分号分隔你的sql就行了,另外切换数据库什么在程序里也是不行的,但如果你的帐号有跨库权限,那么可以直接通过库.用户.表名的方式访问,不过不建议这么做
秋的红果实 2016-08-16
  • 打赏
  • 举报
回复
那不是提示:go附近有语法错误吗? 注意:go是sql ManageMent Tool里的关键字,不是T-SQL的语法成分,不能提交执行,例如你的 begin DROP VIEW sysproperties; end go CREATE VIEW sysproperties AS SELECT class AS id,Minor_id AS sMallid,* from sys.extended_properties; go 只有在管理器中执行,可以有go,否则不可
Justin-Liu 2016-08-16
  • 打赏
  • 举报
回复
go附近有语法错误 你调试的时候 把commandtext拿出来去sql management tool里执行一下看看有没有错误
这是一个比较复杂的数据库 包含图书管理 借书还书 学生管理 老师管理 和数据连接的相关应用 代码有详细的解释 压缩包里面也有 数据库的文件 代码里设置的数据库 用户是 sa 密码是 123456 请使用的时候做相关的修改 下面给出 一部分的代码 请继续关注本资源的发布 会后面有很多实用的代码上传 using System.Windows.Forms; namespace LibraryMis { public class DatabaseAccess { /* 声明成员变量,这样这个类中的所有方法就可是使用这些变量了 */ private SqlConnection myConnection; private SqlCommand myCommand; private SqlDataAdapter myDataAdapter; private DataSet mySet = new DataSet(); /* 写该类的构造方法,该方法名要跟类名相同,无返回值 * 当new这个类时就会执行这个构造方法 */ public DatabaseAccess() { /* 获得保存连接字符串的文件名及路径 */ //获得应用程序路径 string exePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //根据路径和文件名构建FileInfo对象 string fileName = exePath + "connectionString.txt"; //建立FileInfo对象 FileInfo f = new FileInfo(fileName); //判断该文件是否存在 if (f.Exists)//文件存在 { //获得文件内容即存在文件中的连接字符串 //打开文件,获得读文件的数据流对象 StreamReader sr = f.OpenText(); //读文件到变量中 string connectionString = sr.ReadToEnd(); //关闭流 sr.Close(); //由读出的连接字符串创建Connection对象 myConnection = new SqlConnection(connectionString); //由Connection对象创建Command对象 myCommand = myConnection.CreateCommand(); //创建DataAdapter对象 myDataAdapter = new SqlDataAdapter(); myDataAdapter.SelectCommand = myCommand; //创建CommandBuilder对象 SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter); //尝试是否能够打开连接 try { myConnection.Open(); } catch (Exception ex) //打开连接出错,可能是连接字符串有问题,这里调用数据库访问设置窗体来重新设置服务器名和数据库名 { MessageBox.Show("连接不到数据库LibraryMis,请在“数据库访问设置窗体中对数据库访问进行正确的设置”" + ",取消登录后重新启动图书馆管理系统!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning ); //创建 数据库访问设置窗体,并显示 FormSetDatabase fmsd = new FormSetDatabase(); fmsd.ShowDialog(); } finally { try { myConnection.Close(); } catch (Exception ex) { } } return; } else //文件不存在 { //设置默认的连接字符串 string connectionString = "server=.;database=LibraryMis;uid=sa;pwd=123456"; //把这个字符串写入文件 StreamWriter sw = new StreamWriter(fileName); sw.Write(connectionString); sw.Close(); MessageBox.Show("文件" + fileName + "不存在,已创建该文件,请重新启动图书馆管理系统","警告",MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } /*创建查询的方法,返回数据集对象DataSet,参数SelectString表示查询的Sql语句,TableName表示要查询的表名*/ public DataSet FillDataSet(string SelectString, string TableName) { myDataAdapter.SelectCommand.CommandText = SelectString;//设置查询的Sql语句 myDataAdapter.Fill(mySet,TableName); return mySet; } /*执行插入,更新,修改的操作,参数CommandString表示Sql语句*/ public void ExeCommand(string CommandString) { myCommand.CommandText = CommandString; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(),"警告",MessageBoxButtons.OK,MessageBoxIcon.Warning); } finally { myConnection.Close(); } } /*执行存储过程的方法,参数为Command对象*/ public void ExeStoreProcedure(SqlCommand command) { command.Connection = myConnection; myCommand = command; myConnection.Open(); try { myCommand.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } finally { myConnection.Close(); } }

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧