如何从 Sybase ASE 15(使用 ODBC)执行存储过程并使用 C# 获取 DataSet?
public static object GetDataSetOdbc(string conn, string query)
{
OdbcConnection ConexaoOdbc = new OdbcConnection(conn);
ConexaoOdbc.ConnectionTimeout = 120;
OdbcDataAdapter daOdbc;
OdbcCommand cmdOdbc;
DataSet dsResultado = new DataSet();
bool exc = false;
try
{
if (query != "")
{
cmdOdbc = new OdbcCommand(query, ConexaoOdbc);
cmdOdbc.CommandType = CommandType.Text;
daOdbc = new OdbcDataAdapter(cmdOdbc);
ConexaoOdbc.Open();
int idx = VerificaExpressao(query);
if (idx < 0)
cmdOdbc.ExecuteNonQuery();
else
{
cmdOdbc.ExecuteScalar();
}
daOdbc.Fill(dsResultado, "result");
}
else
ConexaoOdbc.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
exc = true;
}
finally
{
ConexaoOdbc.Close();
}
if (exc)
return "";
else
return dsResultado;
}
private static int VerificaExpressao(string query)
{
int idx = query.IndexOf("Create", StringComparison.CurrentCultureIgnoreCase);
if (idx == -1)
idx = query.IndexOf("Drop", StringComparison.CurrentCultureIgnoreCase);
if (idx == -1)
idx = query.IndexOf("Alter", StringComparison.CurrentCultureIgnoreCase);
if (idx == -1)
idx = query.IndexOf("Insert", StringComparison.CurrentCultureIgnoreCase);
if (idx == -1)
idx = query.IndexOf("Update", StringComparison.CurrentCultureIgnoreCase);
if (idx == -1)
idx = query.IndexOf("Exec", StringComparison.CurrentCultureIgnoreCase);
return idx;
}