001 1.C#连接连接Access |
002 | 程序代码: |
003 |
004 | |
005 |
006 | using System.Data; |
007 | using System.Data.OleDb; |
008 |
009 |
010 | string strConnection= "Provider=Microsoft.Jet.OleDb.4.0;" ; |
011 | strConnection+= @"Data Source=C:BegASPNETNorthwind.mdb" ; |
012 |
013 | OleDbConnection objConnection= new OleDbConnection(strConnection); |
014 |
015 |
016 | objConnection.Open(); |
017 | 12objConnection.Close(); |
018 | |
019 |
020 | 解释: |
021 |
022 | 连接Access数据库需要导入额外的命名空间,所以有了最前面的两条 using 命令,这是必不可少的! |
023 |
024 | strConnection这个变量里存放的是连接数据库所需要的连接字符串,他指定了要使用的数据提供者和要使用的数据源. |
025 |
026 | "Provider=Microsoft.Jet.OleDb.4.0;" 是指数据提供者,这里使用的是Microsoft Jet引擎,也就是Access中的数据引擎,asp.net就是*这个和Access的数据库连接的. |
027 |
028 | "Data Source=C:/BegASPNET/Northwind.mdb" 是指明数据源的位置,他的标准形式是 "Data Source=MyDrive:MyPath/MyFile.MDB" . |
029 |
030 | PS: |
031 | 1. "+=" 后面的 "@" 符号是防止将后面字符串中的"/"解析为转义字符. |
032 | 2.如果要连接的数据库文件和当前文件在同一个目录下,还可以使用如下的方法连接: |
033 | strConnection+= "Data Source=" ; |
034 | strConnection+=MapPath( "Northwind.mdb" ); |
035 | 这样就可以省得你写一大堆东西了! |
036 | 3.要注意连接字符串中的参数之间要用分号来分隔. |
037 |
038 | "OleDbConnection objConnection=new OleDbConnection(strConnection);" 这一句是利用定义好的连接字符串来建立了一个链接对象,以后对数据库的操作我们都要和这个对象打交道. |
039 |
040 | "objConnection.Open();" 这用来打开连接.至此,与Access数据库的连接完成. |
041 |
042 | |
043 |
044 | 2.C#连接SQL Server |
045 | 程序代码: |
046 |
047 | |
048 |
049 | using System.Data; |
050 | using System.Data.SqlClient; |
051 |
052 |
053 | string strConnection= "user id=sa;password=;" ; |
054 | strConnection+= "initial catalog=Northwind;Server=YourSQLServer;" ; |
055 | strConnection+= "Connect Timeout=30" ; |
056 |
057 | SqlConnection objConnection= new SqlConnection(strConnection); |
058 |
059 |
060 | objConnection.Open(); |
061 | objConnection.Close(); |
062 | |
063 |
064 | 解释: |
065 |
066 | 连接SQL Server数据库的机制与连接Access的机制没有什么太大的区别,只是改变了Connection对象和连接字符串中的不同参数. |
067 |
068 | 首先,连接SQL Server使用的命名空间不是 "System.Data.OleDb" ,而是 "System.Data.SqlClient" . |
069 |
070 | 其次就是他的连接字符串了,我们一个一个参数来介绍(注意:参数间用分号分隔): |
071 | "user id=sa" :连接数据库的验证用户名为sa.他还有一个别名 "uid" ,所以这句我们还可以写成 "uid=sa" . |
072 | "password=" :连接数据库的验证密码为空.他的别名为 "pwd" ,所以我们可以写为 "pwd=" . |
073 | 这里注意,你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用 "user id" 和 "password" 这样的方式来登录,而需要使用 "Trusted_Connection=SSPI" 来进行登录. |
074 | "initial catalog=Northwind" :使用的数据源为 "Northwind" 这个数据库.他的别名为 "Database" ,本句可以写成 "Database=Northwind" . |
075 | "Server=YourSQLServer" :使用名为 "YourSQLServer" 的服务器.他的别名为 "Data Source" , "Address" , "Addr" .如果使用的是本地数据库且定义了实例名,则可以写为 "Server=(local)/实例名" ;如果是远程服务器,则将 "(local)" 替换为远程服务器的名称或IP地址. |
076 | "Connect Timeout=30" :连接超时时间为30秒. |
077 |
078 | 在这里,建立连接对象用的构造函数为:SqlConnection. |
079 |
080 | |
081 |
082 | 3.C#连接Oracle |
083 | 程序代码: |
084 |
085 | |
086 |
087 | using System.Data.OracleClient; |
088 | using System.Data; |
089 |
090 | //在窗体上添加一个按钮,叫Button1,双击Button1,输入以下代码 |
091 | private void Button1_Click( object sender, System.EventArgs e) |
092 | { |
093 | string ConnectionString= "Data Source=sky;user=system;password=manager;" ; //写连接串 |
094 | OracleConnection conn= new OracleConnection(ConnectionString); //创建一个新连接 |
095 | try |
096 | { |
097 | conn.Open(); |
098 | OracleCommand cmd=conn.CreateCommand(); |
099 |
100 | cmd.CommandText= "select * from MyTable" ; //在这儿写sql语句 |
101 | OracleDataReader odr=cmd.ExecuteReader(); //创建一个OracleDateReader对象 |
102 | while (odr.Read()) //读取数据,如果odr.Read()返回为false的话,就说明到记录集的尾部了 |
103 | { |
104 | Response.Write(odr.GetOracleString(1).ToString()); //输出字段1,这个数是字段索引,具体怎么使用字段名还有待研究 |
105 | } |
106 | odr.Close(); |
107 | } |
108 | catch (Exception ee) |
109 | { |
110 | Response.Write(ee.Message); //如果有错误,输出错误信息 |
111 | } |
112 | finally |
113 | { |
114 | conn.Close(); //关闭连接 |
115 | } |
116 | } |
117 | |
118 |
119 | 4.C#连接MySQL |
120 | 程序代码: |
121 |
122 | |
123 |
124 | using MySQLDriverCS; |
125 |
126 | // 建立数据库连接 |
127 | MySQLConnection DBConn; |
128 | DBConn = new MySQLConnection( new MySQLConnectionString( "localhost" , "mysql" , "root" , "" ,3306).AsString); |
129 | DBConn.Open(); |
130 |
131 | // 执行查询语句 |
132 | MySQLCommand DBComm; |
133 | DBComm = new MySQLCommand( "select Host,User from user" ,DBConn); |
134 |
135 | // 读取数据 |
136 | MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); |
137 |
138 | // 显示数据 |
139 | try |
140 | { |
141 | while (DBReader.Read()) |
142 | { |
143 | Console.WriteLine( "Host = {0} and User = {1}" , DBReader.GetString(0),DBReader.GetString(1)); |
144 | } |
145 | } |
146 | finally |
147 | { |
148 | DBReader.Close(); |
149 | DBConn.Close(); |
150 | } |
151 |
152 | //关闭数据库连接 |
153 | DBConn.Close(); |
154 | |
155 |
156 | 5.C#连接IBM DB2 |
157 | 程序代码: |
158 |
159 | |
160 |
161 | OleDbConnection1.Open(); |
162 | //打开数据库连接 |
163 | OleDbDataAdapter1.Fill(dataSet1, "Address" ); |
164 | //将得来的数据填入dataSet |
165 | DataGrid1.DataBind(); |
166 | //绑定数据 |
167 | OleDbConnection1.Close(); |
168 | //关闭连接 |
169 |
170 | //增加数据库数据 |
171 | 在Web Form上新增对应字段数量个数的TextBox,及一个button,为该按键增加Click响应事件代码如下: |
172 |
173 | this .OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME, |
174 | EMAIL, AGE, ADDRESS) VALUES |
175 | ( '"+TextBox1.Text+"' , '"+TextBox2.Text+"' , '"+TextBox3.Text+"' , '"+TextBox4.Text+"' )"; |
176 | OleDbInsertCommand1.Connection.Open(); |
177 | //打开连接 |
178 | OleDbInsertCommand1.ExecuteNonQuery(); |
179 | //执行该SQL语句 |
180 | OleDbInsertCommand1.Connection.Close(); |
181 | //关闭连接 |
182 | |
183 |
184 | 6.C#连接SyBase |
185 | 程序代码: (OleDb) |
186 |
187 | |
188 |
189 | Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties= "" ;Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000; |