通过ODBC连接到SQL SERVER的两种方法:
一、通过加载SQL SERVER驱动方式;
二、通过配置DSN名称;
string constr = "driver={sql server};database=" + databaseNAME+ ";server=" + 服务器地址 + ";"//SQL SERVER驱动 constr += "UID=invuser;PWD=1;" OdbcCommand cmd_s = new OdbcCommand(sSQL, conn_odbc); OdbcDataReader reader_s = cmd_s.ExecuteReader();//执行查询 int iCZ = 0; while (reader_s.Read()) { JLBH_OUT.Add(data.iJLBH); iCZ=1; } reader_s.Close(); sSQL = " insert into dbo(操作员名称,导出日期) values "; string stmp = "("; stmp += "'" + sDJRMC + "',getdate()"; stmp += ")"; sSQL += stmp; OdbcCommand cmd_in = new OdbcCommand(sSQL, conn_odbc); int iInsert = cmd_in.ExecuteNonQuery(); //执行插入 conn_odbc.Close();
OdbcConnection cn; OdbcCommand cmd; string MyString; MyString="Select * from dbo"; cn= new OdbcConnection("dsn=myDSN;UID=invuser;PWD=1;");//通过DSN cmd=new OdbcCommand(MyString,cn); cn.Open(); MessageBox.Show("Connected"); cn.Close();