C# -- 使用ODBC连接数据库

C# -- 使用ODBC连接数据库

    public class ODBCHelper
    {
        public static string conString1 = "Dsn=sqlServerDsn;DataBase=Test;Uid=User1;Pwd=123456;";
        public static string conString2 = "Dsn=oracleDsn;Uid=User1;Pwd=123456;";

        public DataTable SQLServerExeQuery(string sql)
        {
            using (OdbcConnection con = new OdbcConnection(conString1))
            {
                using (OdbcCommand cmd = new OdbcCommand(sql, con))
                {
                    using (OdbcDataAdapter da = new OdbcDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        con.Open();
                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        public DataTable OracleExeQuery(string sql)
        {
            using (OdbcConnection con = new OdbcConnection(conString2))
            {
                using (OdbcCommand cmd = new OdbcCommand(sql, con))
                {
                    using (OdbcDataAdapter da = new OdbcDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        con.Open();

                        da.Fill(dt);
                        return dt;
                    }
                }
            }
        }

    }


ODBC DSN配置:

posted on 2019-06-30 18:13  在代码的世界里游走  阅读(9218)  评论(0编辑  收藏  举报