Example Method for C to Connect to a Database Using ODBC and OLEDB
- 2021-12-13 16:42:50
- OfStack
This article illustrates how C # uses ODBC and OLEDB to connect to a database. Share it for your reference, as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;
using System.Data.OleDb;
namespace ODBCtest
{
class Program
{
static void Main(string[] args)
{
//ODBC Connect
string conString = "DSN=tian"; //tian Denote ODBC User data source name of
string sql = "select count(*) from stuinfo"; //stuinfo In the database bound for the user data source 1 Table
OdbcConnection con = new OdbcConnection(conString);
con.Open();
OdbcCommand com = new OdbcCommand(sql, con);
int i = Convert.ToInt32(com.ExecuteScalar());
Console.WriteLine(i);
//OLEDB Connect
string conString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\tian.mdb"; // Connect Access Database
string sql = "select count(*) from stuinfo";
OleDbConnection con = new OleDbConnection(conString);
con.Open();
OleDbCommand com = new OleDbCommand(sql, con);
int i = Convert.ToInt32(com.ExecuteScalar());
Console.WriteLine(i);
}
}
}
For more readers interested in C # related content, please check the topics on this site: "Summary of Thread Use Skills in C # Programming", "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"
I hope this article is helpful to everyone's C # programming.