惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Martin Fowler
Martin Fowler
H
Help Net Security
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
Kaspersky official blog
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 最新话题
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
AI
AI
The Hacker News
The Hacker News
Latest news
Latest news
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
SecWiki News
SecWiki News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
Attack and Defense Labs
Attack and Defense Labs
Security Latest
Security Latest
Help Net Security
Help Net Security
博客园 - Franky
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
Cloudbric
Cloudbric
P
Proofpoint News Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 【当耐特】
Schneier on Security
Schneier on Security
Google Online Security Blog
Google Online Security Blog
C
Cisco Blogs
Webroot Blog
Webroot Blog
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
A
Arctic Wolf
O
OpenAI News
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Last Watchdog
The Last Watchdog

博客园 - 寸芒

[原创]作业:家庭消费管理程序,一点代码 [原创]JScript学习摘要1 [随]程序设计模式的有趣解释-追MM [随]SQL Server 和ACCESS 、EXCEL 的数据转换 [原创]treeview简单根据数据库的数据,显示多级节点 [随]测试了下我的Blog价值(汗颜了) [随]C#轻松解决世纪迷题 [随]下了个windows live write 来玩玩 [随] 网站注册随机码的实现 [随]存储过程入门与提高 [原创]涂鸦事件 [随]c#.net常用函数和方法 [原创]到底调用哪个方法 [原创]浅要分析委托和事件 [原创]readonly 和const分析 [随]抽象基类与接口,共性与个性的选择 [原创]Box and Unbox [随]八荣八耻 [原创]引用类型的转换之感想
[随]c#连接数据库
寸芒 · 2007-08-04 · via 博客园 - 寸芒

(来自Google搜索)

1

.C#连接连接Access
程序代码:
-------------------------------------------------------------------------------
using System.Data;
using System.Data.OleDb;
..
string strConnection="Provider=Microsoft.Jet.OleDb.4.0;";
strConnection
+=@"Data Source=C:BegASPNETNorthwind.mdb";

OleDbConnection objConnection

=new OleDbConnection(strConnection);
..

objConnection.Open();
objConnection.Close();

--------------------------------------------------------------------------------

解释:

 连接Access数据库需要导入额外的命名空间,所以有了最前面的两条using命令,这是必不可少的

!

 strConnection这个变量里存放的是连接数据库所需要的连接字符串,他指定了要使用的数据提供者和要使用的数据源.

"Provider=Microsoft.Jet.OleDb.4.0;"是指数据提供者,这里使用的是Microsoft Jet引擎,也就是Access中的数据引擎,asp.net就是靠这个和Access的数据库连接的."Data Source=C:\BegASPNET\Northwind.mdb"是指明数据源的位置,他的标准形式是"Data Source=MyDrive:MyPath\MyFile.MDB".

PS:

1."+="后面的"@"符号是防止将后面字符串中的"\"解析为转义字符.
 
2.如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接:
  strConnection
+="Data Source=";
  strConnection
+=MapPath("Northwind.mdb";
  这样就可以省得你写一大堆东西了
!
 
3.要注意连接字符串中的参数之间要用分号来分隔."OleDbConnection objConnection=new OleDbConnection(strConnection);"这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的操作我们都要和这个对象打交道."objConnection.Open();"这用来打开连接.至此,与Access数据库的连接完成.
--------------------------------------------------------------------------------2.C#连接SQL Server
程序代码:
--------------------------------------------------------------------------------using System.Data;
using System.Data.SqlClient;
..
string strConnection="user id=sa;password=;";
strConnection
+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection
+="Connect Timeout=30";

SqlConnection objConnection

=new SqlConnection(strConnection);
..

objConnection.Open();
objConnection.Close();

--------------------------------------------------------------------------------

解释:

连接SQL Server数据库的机制与连接Access的机制没有什么太大的区别,只是改变了Connection对象和连接字符串中的不同参数.

首先,连接SQL Server使用的命名空间不是

"System.Data.OleDb",而是"System.Data.SqlClient".

其次就是他的连接字符串了,我们一个一个参数来介绍(注意:参数间用分号分隔):

"user id=sa":连接数据库的验证用户名为sa.他还有一个别名"uid",所以这句我们还可以写成"uid=sa".
 
"password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
 这里注意,你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用
"user id""password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
 
"initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
 
"Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
 
"Connect Timeout=30":连接超时时间为30秒.

 在这里,建立连接对象用的构造函数为:SqlConnection.

3.C#连接Oracle
程序代码:
--------------------------------------------------------------------------------
using System.Data.OracleClient;
using System.Data;//在窗体上添加一个按钮,叫Button1,双击Button1,输入以下代码
private void Button1_Click(object sender, System.EventArgs e)
{
string ConnectionString="Data Source=sky;user=system;password=manager;";//写连接串
OracleConnection conn=new OracleConnection(ConnectionString);//创建一个新连接
try
{
conn.Open();
OracleCommand cmd
=conn.CreateCommand();

cmd.CommandText

="select * from MyTable";//在这儿写sql语句
OracleDataReader odr=cmd.ExecuteReader();//创建一个OracleDateReader对象
while(odr.Read())//读取数据,如果odr.Read()返回为false的话,就说明到记录集的尾部了                
{
Response.Write(odr.GetOracleString(
1).ToString());//输出字段1,这个数是字段索引,具体怎么使用字段名还有待研究
}
odr.Close();
}
catch(Exception ee)
{
Response.Write(ee.Message); 
//如果有错误,输出错误信息
}
finally
{
conn.Close(); 
//关闭连接
}
}
---------------------------------------------------------------
4.C#连接MySQL
程序代码:
--------------------------------------------------------------------------------using MySQLDriverCS;// 建立数据库连接
MySQLConnection DBConn;
DBConn 
= new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString);
DBConn.Open();
// 执行查询语句
MySQLCommand DBComm;
DBComm 
= new MySQLCommand("select Host,User from user",DBConn);// 读取数据
MySQLDataReader DBReader = DBComm.ExecuteReaderEx();// 显示数据
try
{
while (DBReader.Read())
{
Console.WriteLine(
"Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
{
DBReader.Close();
DBConn.Close();
}
//关闭数据库连接
DBConn.Close();
--------------------------------------------------------------------------------5.C#连接IBM DB2
程序代码:
--------------------------------------------------------------------------------

OleDbConnection1.Open();

//打开数据库连接
OleDbDataAdapter1.Fill(dataSet1,"Address";
//将得来的数据填入dataSet
DataGrid1.DataBind();
//绑定数据
OleDbConnection1.Close();
//关闭连接//增加数据库数据
在Web Form上新增对应字段数量个数的TextBox,及一个button,为该按键增加Click响应事件代码如下:this.OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME,
EMAIL, AGE, ADDRESS) VALUES
(
'"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
OleDbInsertCommand1.Connection.Open();
//打开连接
OleDbInsertCommand1.ExecuteNonQuery();
//执行该SQL语句
OleDbInsertCommand1.Connection.Close();
//关闭连接
--------------------------------------------------------------------------------6.C#连接SyBase
程序代码: (OleDb)
--------------------------------------------------------------------------------

Provider

=Sybase.ASEOLEDBProvider.2;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;