漫谈 ADO.NET之四大核心对象

来源:互联网 发布:网络拍卖平台靠谱 编辑:程序博客网 时间:2024/05/29 16:55

本人热爱Java,可最终做了c#工作,人生之一大憾事,俗话说,水浅王八多,遍地是大哥,也只好苟存于社会的残垣瓦砾之下!

下面是第一次使用四个“小对象”的具体实现代码

(Oracle 数据库)

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.OracleClient;using System.Data.OleDb;using System.Data;namespace ConnectionOracle{    class Program    {        static void Main(string[] args)        {            string connString = "Provider=OraOLEDB.Oracle;Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)    (HOST = 10.133.12.99)(PORT = 1521)))(CONNECT_DATA = (SERVICE_NAME = 'dxysdb')));  User ID ='interact';  Password = 'test' ";            //OracleConnection conn = new OracleConnection(connString);            OleDbConnection conn = new OleDbConnection(connString); ;            conn.Open();            try            {                String strsql = "select * from ts_user where userid = '21011223'";                OleDbCommand oc = conn.CreateCommand();                //默认情况下CommandType的属性是CommandText,执行一般的SQL命令,                //但是如果需要执行存储的时候需要更改属性为CommandType.StoredProcedure                //oc.CommandType = CommandType.Text;                //oc.CommandType = CommandType.StoredProcedure;                oc.CommandText = strsql;                int odr = oc.ExecuteNonQuery();//返回影响的行数                Console.WriteLine("影响的行数:{0}", odr);                OleDbDataReader read = oc.ExecuteReader();                /*                因为DataReader对象读取数据时需要与数据库保持连接,所以在使用完DataReader对象读取完数据之后应该立即调用它的Close()方法关闭,                并且还应该关闭与之相关的Connection对象。在.net类库中提供了一种方法,                在关闭DataReader对象的同时自动关闭掉与之相关的Connection对象,使用这种方法是可以为ExecuteReader()方法指定一个参数,如:                SqlDataReader reader =command.ExecuteReader(CommandBehavior.CloseConnection);                CommandBehavior是一个枚举,上面使用了CommandBehavior枚举的CloseConnection值,它能在关闭SqlDataReader时关闭相应的SqlConnection对象。               */                  if (read != null && read.HasRows)                {                    while (read.Read())                    {                        for (int i = 0; i < read.FieldCount; ++i)                        {                            Console.WriteLine("{0}:{1}", read.GetName(i), read.GetValue(i));                        }                    }                }                read.Close();                Console.WriteLine("----------------温柔的分割线-------------------");                OleDbDataAdapter adapter = new OleDbDataAdapter(oc);                DataTable table = new DataTable();                adapter.Fill(table);                for (int i = 0; i < table.Rows.Count;i++ )                {                    string name = table.Rows[i]["username"].ToString();                    string userid = table.Rows[i]["userid"].ToString();                    string deptname = table.Rows[i]["deptname"].ToString();                    Console.WriteLine("名字:{0},工号:{1},部门:{2}",name,userid,deptname);                }                string namesd = table.TableName.ToString();                Console.WriteLine("the table name isn't be gieven ,but it already be named,it is :{0}",namesd);            }            catch (Exception e)            {                Console.WriteLine(e.Message);            }            finally            {                conn.Close();                conn.Dispose();            }            Console.ReadKey();        }    }}


0 0
原创粉丝点击