一.用SqlConnection连接SQL Server
1..加入命名空间
using System.Data.SqlClient;
2.连接数据库
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "user id=sa;password=sinofindb;initial catalog=test;data source=127.0.0.1;Connect Timeout=30";
myConnection.Open();
改进(更通用)的方法:
string MySqlConnection="user id=sa;password=sinofindb;Database =test;data source=127.0.0.1;Connect Timeout=30";
SqlConnection myConnection = new SqlConnection(MySqlConnection);
myConnection.Open();
二。用OleDbConnection连接
1.加入命名空间
using System.Data.OleDb;
2.连接sql server
string MySqlConnection="Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=test;Integrated Security=SSPI;";
SqlConnection myConnection = new SqlConnection(MySqlConnection);
myConnection.Open();
3.连接Access(可通过建立.udl文件获得字符串)
string MySqlConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/db2000.mdb;
Persist Security Info=False;
4.连接Oracle(也可通过OracleConnection连接)
string MySqlConnection="Provider=MSDAORA;Data Source=db; user id=sa;password=sinofindb";
三.创建Command对象
1.SqlCommand 构造函数
①初始化 SqlCommand 类的新实例。public SqlCommand();
SqlCommand myCommand = new SqlCommand();
②初始化具有查询文本的 SqlCommand 类的新实例。public SqlCommand(string);
<
本文详细介绍了如何使用ADO.NET连接SQL Server、OleDbConnection连接不同数据库,以及SqlCommand对象的使用,包括创建Command对象、执行命令。还讨论了DataReader的使用、DataAdapter的填充和DataTable的操作,展示了数据库操作的基本步骤和方法。
最低0.47元/天 解锁文章
4209

被折叠的 条评论
为什么被折叠?



