C#数据库操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;//连接Access数据库
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;//调试输出Trace.WriteLine

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //连接数据库
        OleDbConnection conn;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mine.mdb;Persist Security Info=False";
                conn = new OleDbConnection(ConStr);
                conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    label1.Text = "连接数据库成功";
                }
            }
            catch
            {
                MessageBox.Show("连接数据库失败");
            }
        }
        //关闭数据库
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                conn.Close();
                if (conn.State == ConnectionState.Closed)
                {
                    label1.Text = "关闭数据库成功";
                }
            }
            catch
            {
                MessageBox.Show("关闭数据库失败");
            }
        }
        //Command的用法--执行SQL语句
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();             
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO info (Name, Age,Place) VALUES ('Wilson', '12','beijing')";
                cmd.CommandType = CommandType.Text;
                //int i = Convert.ToInt32(cmd.ExecuteNonQuery());
                int i = cmd.ExecuteNonQuery();//返回受影响的行数
                string iStr = string.Format("有{0}行受影响", i.ToString());
                toolStripStatusLabel1.Text = iStr;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //DataReader对象是一个简单的数据集,用于从数据源中检索只读数据集,常用于检索大量数据。
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.CommandText = "select * from info";
                cmd.CommandType = CommandType.Text;
                OleDbDataReader drNew = cmd.ExecuteReader();
                string[] parm=new string[4];
                if (drNew.HasRows)
                {
                    while (drNew.Read())
                    {
                        Trace.WriteLine(drNew[1]);
                    }
                }
                drNew.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //DataSet的创建。可以把DataSet当成内存中的数据库,DataSet是不依赖于数据库的独立数据集合
        DataSet ds;
        int i = 0;//当前行
        int j = 0;//行数
        DataTable dtInfo;
        private void DisPlay()
        {
            DataRow dr = dtInfo.Rows[i];
            textID.Text = dr[0].ToString();
            textName.Text = dr[1].ToString();
            textAge.Text = dr[2].ToString();
            textPlace.Text = dr[3].ToString();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                ds = new DataSet();
                OleDbDataAdapter daNew = new OleDbDataAdapter("select * from info", conn);
                daNew.Fill(ds);
                toolStripStatusLabel1.Text = "创建DataSet成功!";
            }
            catch(Exception ex)
            {
                toolStripStatusLabel1.Text = ex.Message;
            }
            dtInfo = ds.Tables[0];
            foreach (DataColumn dc in dtInfo.Columns)
                Trace.WriteLine(dc.ColumnName);//调试输出列名
            foreach (DataRow row in dtInfo.Rows)
                Trace.WriteLine(row[0]);//调试输出第一列数据
            
            j = dtInfo.Rows.Count;//获取数据库的行数
            DisPlay();
        }
        //向上翻页
        private void button6_Click(object sender, EventArgs e)
        {
            if (i > 0)
            {
                i--;
                DisPlay();
            }
            else
            {
                toolStripStatusLabel1.Text = "当前为第一页!";
            }
        }
        //向下翻页
        private void button7_Click(object sender, EventArgs e)
        {
            if (i < j-1)
            {
                i++;
                DisPlay();
            }
            else
                toolStripStatusLabel1.Text = "已到尾页!";
        }
        //添加数据
        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                string cmdStr = string.Format("INSERT INTO info (Name,Age,Place) VALUES ('{0}','{1}','{2}')", textName.Text, textAge.Text, textPlace.Text);
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = cmdStr;
                int i = cmd.ExecuteNonQuery();//返回受影响的行数
                toolStripStatusLabel1.Text="添加数据成功!";
                //更新数据库,与之前代码一样的
                try
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                        string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mine.mdb;Persist Security Info=False";
                        conn = new OleDbConnection(ConStr);
                        conn.Open();
                    }
                }
                catch
                {
                    MessageBox.Show("连接数据库失败");
                }
                try
                {
                    ds = new DataSet();
                    OleDbDataAdapter daNew = new OleDbDataAdapter("select * from info", conn);
                    daNew.Fill(ds);
                    toolStripStatusLabel1.Text = "创建DataSet成功!";
                }
                catch (Exception ex)
                {
                    toolStripStatusLabel1.Text = ex.Message;
                }
                dtInfo = ds.Tables[0];
                foreach (DataColumn dc in dtInfo.Columns)
                    Trace.WriteLine(dc.ColumnName);//调试输出列名
                foreach (DataRow row in dtInfo.Rows)
                    Trace.WriteLine(row[0]);//调试输出第一列数据

                j = dtInfo.Rows.Count;//获取数据库的行数
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
程序打包下载地址:http://pan.baidu.com/s/1dDuw84h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值