`
baobeituping
  • 浏览: 1043223 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

c# 链接数据库

    博客分类:
  • .NET
阅读更多

用C#做了些应用,现对ADO.NET的数据库访问作一个小结,以供大家交流学习,迟些时候贴上自己的一些代码。
  ADO.NET的数据访问对象是以下几个:
  1、Connection对象:与数据源建立连接,连接sql server7.0 或更新版本数据库用SqlConnection,连接OLEDB数据源使用OledbConnection.
  2、Command 对象:对数据源执行SQL命令并返回结果,SQL Server7.0或更新版本用SqlCommand,OLE DB数据源使用OledbCommand.
  3、DataReader对象: 读取数据源的数据,只能将数据源的数据从头到尾依次读出,Sql server7.0或以上版本使用SqlDataReader,Oledb数据源使用OledbReader
  4、DataAdapter对象:对数据源执行操作并返回结果,在DataSet与数据源之间建立通信,将数据源中的数据写入DataSet ,或根据DataSet中的数据必定数据源。Sql server7.0或以上版本使用SqlDataAdapter,Oledb 数据源使用OledbAdpater.
  5、DataSet对象:服务器内存中的数据库
  6、DataView对象:用于显示DataSet中的数据

  从数据库中读取纪录的另一种方法是使用Dataset对象和Dataadapter对象.Dataset是ADO.NET的主要组件之一,它用于缓存从数据源检索到的数据信息。Dataadapter作为Dataset和数据源之间的桥接器,用于检索和保存数据。Dataadapter从数据库中获取数据后使用Fill方法把数据填充到Dataset中。下面以Sqldataadapter为例说明如何使用Dataset对象和Dataadapter对象从数据库中读取记录。执行查询的关键步骤如下:
  1、创建与数据库建立连接的Sqlconnection,传递连接字符串。
  2、构造包含查询语句的Sqldataadapter对象;
  3、若要使用查询结果填充Dataset对象,则调用命令Fill方法。


  c#利用ado.net进行数据库开发的基本步骤:
  1、创建和数据库连接的connection 对象。
  2、配置DataAdapter对象并创建和操作数据集DataSet。
  3、将数据库中的表添加到DataSet中。
  4、把数据集DataSet绑定到DataGrid上。利用DataAdapter 的Fill方法把数据填充到DataSet,最终的数据库中的数据显示在用户界面的DataGrid中。


  c#中从数据库查询记录的方法分类:
        一般使用两种方法:
        一种是通过DataReader对象直接访问;另一种则是通过数据集Dataset和Dataadapter对象访问.
  使用ADO.NET的Datareader对象能从数据库中检索数据。检索出来的数据形成一个只读只进的数据流,存储在客户端的网络缓冲区内。Datareader对象的read方法可以前进到一下条记录。在默认情况下,每执行一次read方法只会在内存中存储一条记录系统的开销非常少。
  创建datareader之前必须先创建sqlcommand对象,然后调用该对象的executereader方法来构造sqldatareader对象,而不是直接使用构造函数。
  下面的示例程序完成的功能是访问sqlserver数据库,并使用datareader从northwind数据中读取记录,并将查询结果通过控制台输出。
  using System;
  using System.Data;
  using System.Data.SqlClient;
  namespace ReadDataFromDB{

  class Class1{

  static void Main(string[] args){
  string myconn="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind";
  //需要执行的SQL语句
  string mysql="select OrderID,CustomerID from Orders where CustomerID='CHOPS'";
  //打开数据库连接。
    SqlConnection myconnection=new SqlConnection(myconn);
  myconnection.Open();
    //创建SqlCommand 对象
  SqlCommand mycommand=new(mysql,myconnection);
    //通过SqlCommand的ExecuteReader()方法构造DataReader 对象。
  SqlDataReader myreader=mycommand.ExecuteReader();
  while(myreader.read()){
  Console.WriteLine(myreader.GetInt32(0)+","+myreader.GetString(1));

  }
  myreader.Close();
  myconnection.Close();

  }

  }

  }

 

 

如果要用到c#链接MYSQL数据库,那么就要下载MySql.Data.dll(附件中有),然后引入到你的工程中。

连接代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace AspDB
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection con = new MySql.Data.MySqlClient.MySqlConnection("Database='test';Data Source='localhost';User Id='root';Password='root';charset='utf8'");
            MySqlCommand commend = new MySqlCommand("select * from user", con);
            con.Open();
          
            if (con.State == System.Data.ConnectionState.Open)
            {
            
                MySqlDataAdapter msda = new MySqlDataAdapter("select * from user",con);
                DataSet ds = new DataSet();
                msda.Fill(ds);
                con.Close();
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();
            }
            else
            {
                Response.Write("<script>alert('数据库连接没打开。');</script>");
            }
        
        }
    }
}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics