连接数据库
SqlConnection
string connstr= "data source=地址; Database=库名;user id=用户名; password=密码";
SqlConnection conn= new SqlConnection(connstr);
conn.open();
OracleConnection
string connstr= "data source=地址; Database=库名;user id=用户名; password=密码";
OracleConnection conn= new OracleConnection(connstr);
conn.open();
MySql/OdbcConnection
要先确认已经安装了MySql-connector-net-5.1.5.Data.msi组件
using MySql.Data.MySqlClient;
string connstr= "server=地址; Database=库名;user id=用户名; password=密码";
MySqlConnection conn= new MySqlConnection(connstr);
conn.open();
OleDbConnection
using System.Data.OleDb;
string connstr= "Provider=SQLOLEDB.1; Persist Security Info=False; user id=用户名; Database=库名; data source=机器名;";
OleDbConnection conn= new OleDbConnection (connstr);
conn.open();
执行Sql命令
无返回
XXXDbCommand command = new XXXDbCommand();
command.ExecuteNonQuery("update");
有返回
XXXDbCommand command = new XXXDbCommand();
System.Collections.IEnumerable result=command.ExecuteQuery("select...");
使用事务
transaction = conn.BeginTransaction();
command.Connection = conn;
command.Transaction = transaction;
command.CommandText ="update..."
command.ExecuteNonQuery();
transaction.Commit();
填充数据集
ds = new DataSet();
conn.Open();
XXXDbDataAdapter command = new XXXDbDataAdapter("select...", conn);
command.Fill(ds, "数据集名称,与表名无关,只用于数据集操作");