1 配置电脑ODBC的需要特别注意32和64位操作系统问题(如果是64位操作系统,这里会有两个ODBC的数据源)
注意:(如果64位配置的,C#代码无法连接,提示sa账号问题,请配置一个32位的ODBC即可!)
2 C# 代码参考:
2.1 连接ODBC 去查询数据
try
{
//DSN:SysDsn32 数据源的名称 UID:sql server登录时的身份sa PWD:登录时的密码123456
//生成连接数据库字符串
string ConStr = "DSN=SysDsn32;UID=sa;PWD=123456";
//定义SqlConnection对象实例
OdbcConnection odbcCon = new OdbcConnection(ConStr);
string SqlStr = "select * from TestInfo";
OdbcDataAdapter odbcAdapter = new OdbcDataAdapter(SqlStr, odbcCon);
DataSet ds = new DataSet();
odbcAdapter.Fill(ds);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
2.2 连接ODBC 执行存储过程( 不带参方式)
string consqlserver = @"DSN=SysDsn32;UID=sa;PWD=Dtgd2018";//UID = sa;PWD = Dtgd2018
OdbcConnection con = new OdbcConnection(consqlserver);
con.Open();
OdbcCommand cmd = con.CreateCommand();
cmd.CommandText = "InserTestdate_Paramter";
cmd.CommandType = CommandType.StoredProcedure;
int result = cmd.ExecuteNonQuery(); 无参数
2.3 连接ODBC 执行存储过程(带参数)
string consqlserver = @"DSN=SysDsn32;UID=sa;PWD=123455";//UID = sa;PWD = 123456
string sql = "SELECT * FROM TestInfo";
OdbcConnection con = new OdbcConnection(consqlserver);
con.Open();
OdbcCommand cmd = con.CreateCommand();
cmd.CommandText = "{CALL InserTestdate_Paramter(?,?)}";
cmd.CommandType = CommandType.StoredProcedure;
OdbcParameter parameter1 = new OdbcParameter("@No", OdbcType.NVarChar);
parameter1.Direction = ParameterDirection.Input;
parameter1.Value = "Paramter_one2";
cmd.Parameters.Add(parameter1);
OdbcParameter parameter2 = new OdbcParameter("@Name", OdbcType.NVarChar);
parameter2.Direction = ParameterDirection.Input;
parameter2.Value = "wangyong";
cmd.Parameters.Add(parameter2);
int result = cmd.ExecuteNonQuery();