手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Mssql>列表

数据库研发个人总结(ADO.NET小结)

来源:互联网 作者:west263.com 时间:2008-04-02
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

一.用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);

String mySelectQuery = "SELECT * FROM mindata";
SqlCommand myCommand = new SqlCommand(mySelectQuery);
③初始化具备查询文本和 SqlConnection 的SqlCommand类实例。

Public SqlCommand(string, SqlConnection);

String mySelectQuery = "SELECT * FROM mindata";
string myConnectString = "user id=sa;password=;database=test;server=mySQLServer";
SqlConnection myConnection = new SqlConnection(myConnectString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection);

④初始化具备查询文本、SqlConnection 和 Transaction 的 SqlCommand 类实例。

public SqlCommand(string, SqlConnection, SqlTransaction);

SqlTransaction myTrans = myConnection.BeginTransaction();
String mySelectQuery = "SELECT * FROM mindata";
string myConnectString = "user id=sa;password=;database=test;server=mySQLServer";
SqlConnection myConnection = new SqlConnection(myConnectString);
SqlCommand myCommand = new SqlCommand(mySelectQuery,myConnection, myTrans);

2.建立SqlCommand和SqlConnection的关联。

myCommand.Connection = myConnection;

或:SqlCommand myCommand = myConnection.CreateCommand;

[1] [2] [3] [4] 下一页


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!