如何通过ODBC连接数据库

时间:2015-01-09 04:15:21

标签: c# odbc

private void Form7_Load(object sender, EventArgs e)
{
    SqlConnection cn = new SqlConnection("Integrated Security=true;database=EDIXfer");
    SqlDataAdapter da = new SqlDataAdapter("select EDIScheduleID from ETAProcessSchedule", cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    for (int x = 0; x < dt.Rows.Count; x++)
    {
        comboBox1.Items.Add(dt.Rows[x][0].ToString());
    }
}

上面的代码工作正常,但是在OLEDB或ODBC的情况下,它不起作用(为OLEDB和ODBC添加了名称空间)。

using System.Data.Odbc;

private void Form7_Load(object sender, EventArgs e)
{
    OdbcConnection cn = new OdbcConnection("Integrated Security=true;database=EDIXfer");
    OdbcDataAdapter da = new OdbcDataAdapter("select EDIScheduleID from ETAProcessSchedule", cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    for (int x = 0; x < dt.Rows.Count; x++)
    {
        comboBox1.Items.Add(dt.Rows[x][0].ToString());
    }
}

如何使用ODBC正确连接数据库?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助,

using (OdbcCommand com = new OdbcCommand(
    "SELECT ColumnWord FROM OkieTable WHERE MagicKey = ?", con))
{
    com.Parameters.AddWithValue("@var", paramWord);

    using (OdbcDataReader reader = com.ExecuteReader())
    {
        while (reader.Read())
        {
            string word = reader.GetString(0);
            // Word is from the database. Do something with it.
        }
    }
}