C#(asp.net)通过odbc方式连接数据库,连接字符串connString ="Driver={SYBASE SYSTEM 11};Srvr=XX;Uid=XX;Pwd=XX;DB=XX",连接正常.
执行查询并返回dataset的函数如下:
public static DataSet OdbcDataSet(string sql, string connString)
{
OdbcConnection connStr = new OdbcConnection(connString);
if (connStr.State.ToString() == "Closed") connStr.Open();
OdbcDataAdapter da = new OdbcDataAdapter(sql, connStr);
DataSet ds = new DataSet();
ds.Clear();
try
{
da.Fill(ds);//(1)报错
}
catch (Exception ex)
{
da.Fill(ds);//(2)第二次执行正常
}
finally
{
da.Dispose();
connStr.Close();
}
return ds;
}
每次执行到(1)位置则出错,错误提示如下:
ERROR [HY000] [INTERSOLV][ODBC SQL Server driver][SQL Server]ct_cursor(CLOSE): user api layer: external error: This type of command cannot be batched with the command already initialized on the command structure.
再执行(2)位置时却正常,如果用全局连接变量也是如此,刚建立连接时第一次执行查询命令时也报上面同样的错误,以后再执行任何正确语句都正常。
有谁知道是什么原因?如何解决?
能解决者高分重谢谢(另开帖给分),讨论皆有分!
------解决方案--------------------------------------------------------
ds.Clear(); 屏蔽掉试一下。没遇到过这种问题
------解决方案--------------------------------------------------------
查询内容是什么?执行其它查询也会出错?
------解决方案--------------------------------------------------------
ase版本多少?与驱动程序是配套的?
------解决方案--------------------------------------------------------
C#我不是太熟悉,但是我想都是面向对象的与java操作方式差不多。
下面给你一个我用java操作sybase的两种连接方式jdbc与jndi,给你提供参照。
- Java code
public class DbConnection { private static String driverName = "com.sybase.jdbc3.jdbc.SybDriver"; private static String url = "jdbc:sybase:Tds:127.0.0.1:5000/project"; private static String user = "sa"; private static String password = ""; private static String jdbcJNDI = "java:comp/env/jdbc/pro"; public DbConnection(){ } public static Connection getConnection1() throws SQLException{ try { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup(jdbcJNDI); return ds.getConnection(); } catch (Exception ex) { ex.printStackTrace(); throw new SQLException(ex.getMessage()); } } public static Connection getConnection() throws SQLException{ try { Class.forName(driverName); return DriverManager.getConnection(url, user, password); } catch (Exception ex) { ex.printStackTrace(); throw new SQLException(ex.getMessage()); } } public static ResultSet select(String sql) throws SQLException{ ResultSet rs = null; Connection conn = null; try { conn = getConnection(); Statement stmt = conn.createStatement(); rs = stmt.executeQuery(sql); } catch (SQLException e) { System.out.println("查询异常:" + e.getMessage()); } finally{ if(conn != null){ conn.close(); } } return rs; }}
------解决方案--------------------------------------------------------
,刚连接每一次会那样的错: