一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

C#在winform中操作數(shù)據(jù)庫,實現(xiàn)數(shù)據(jù)增刪改查

 羊玉wngbx 2019-02-11

1.前言:

運(yùn)行環(huán)境:VS2013+SQL2008+Windows10

程序界面預(yù)覽:

使用的主要控件:dataGridview和menuStrip等。

 

2.功能具體介紹:

1.首先,我們要先實現(xiàn)基本的數(shù)據(jù)操作,增刪改查這幾個操作。

(1)先定義一個數(shù)據(jù)庫操作的公共類:

復(fù)制代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace Data
{
    class SqlDesigner
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["data"].ConnectionString;
        /// <summary>
        /// 返回受影響的數(shù)據(jù)行數(shù)
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int ExecuteNoQuery(string sql)
        {
            using (SqlConnection conn=new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd=conn.CreateCommand())
                {
                  cmd.CommandText = sql;
                  return cmd.ExecuteNonQuery();
                  
                }
            }
        }
        /// <summary>
        /// 返回一個數(shù)據(jù)集
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string sql)
        {
            using (SqlConnection xonn=new SqlConnection(connStr))
            {
                xonn.Open();
                using (SqlCommand cmd = xonn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    return dataset;
                }
            }
        }
        public static object ExecuteScalar(string sql)
        {
            using (SqlConnection conn=new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd=conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    return cmd.ExecuteScalar();
                }
            }
        }
        /// <summary>
        /// md5加密
        /// </summary>
        /// <param name="strPwd"></param>
        /// <returns></returns>
        public static string GetMD5(string strPwd)
        {
            string pwd = "";
            //實例化一個md5對象
            MD5 md5 = MD5.Create();
            // 加密后是一個字節(jié)類型的數(shù)組
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(strPwd));
            //翻轉(zhuǎn)生成的MD5碼        
            s.Reverse();
            //通過使用循環(huán),將字節(jié)類型的數(shù)組轉(zhuǎn)換為字符串,此字符串是常規(guī)字符格式化所得
            //只取MD5碼的一部分,這樣惡意訪問者無法知道取的是哪幾位
            for (int i = 3; i < s.Length - 1; i++)
            {
                //將得到的字符串使用十六進(jìn)制類型格式。格式后的字符是小寫的字母,如果使用大寫(X)則格式后的字符是大寫字符
                //進(jìn)一步對生成的MD5碼做一些改造
                pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString("X");
            }
            return pwd;
        }
    
    }
}
復(fù)制代碼

(2)運(yùn)用建立的公共類,進(jìn)行數(shù)據(jù)庫的操作:

  a.數(shù)據(jù)查詢:
ds = SqlDesigner.ExecuteDataSet("select * from dtuser");            
dt = ds.Tables[0];            
dataGridView1.DataSource = dt;
  b.數(shù)據(jù)添加
 i = SqlDesigner.ExecuteNoQuery("insert into dtuser(uid,uname,pwd,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" +textBox3.Text+ "','" + textBox4.Text + "')");
  c.數(shù)據(jù)刪除
string currentIndex = dataGridView1.CurrentRow.Cells[0].Value.ToString();
i = SqlDesigner.ExecuteNoQuery("delete from dtuser where uid='" + currentIndex + "'");
  d.數(shù)據(jù)修改
i = SqlDesigner.ExecuteNoQuery("update dtrole set rname='" + textBox2.Text + "',flag='" + textBox3.Text + "'where rid='" + textBox1.Text + "'");
  e.一些細(xì)節(jié)

  這里,我們修改一下添加數(shù)據(jù),讓添加的數(shù)據(jù)變成字符串的形式,也就是加密操作:

string str = SqlDesigner.GetMD5(textBox3.Text.Trim());                
 i = SqlDesigner.ExecuteNoQuery("insert into dtuser(uid,uname,pwd,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + str + "','" + textBox4.Text + "')");

 

(3)dataGridView控件:

//綁定數(shù)據(jù)源
dataGridView1.DataSource = dt;
//自動適應(yīng)列寬
dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;

3.代碼僅供參考:

復(fù)制代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Data
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();            
        }                   
                
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        private void TextBoxNull()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
        private void 用戶ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxNull();
            ds = SqlDesigner.ExecuteDataSet("select * from dtuser");            
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
            labelshow();
        }

        private void 角色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxNull();
            ds = SqlDesigner.ExecuteDataSet("select *from dtrole");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
            label4.Text = "None";
            textBox4.Text = "None";
            labelshow();
        }

        private void 對象ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxNull();
            ds = SqlDesigner.ExecuteDataSet("select * from dtfunction");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
            labelshow();
        }

        private void 幫助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBoxNull();
            ds = SqlDesigner.ExecuteDataSet("select * from help");
            dt = ds.Tables[0];
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
        }
        //雙擊dataGridView1
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string index = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            if (label1.Text == "uid")
            {
                ds = SqlDesigner.ExecuteDataSet("select *from dtuser where uid='" + index + "'");
                dt = ds.Tables[0];
                DataRow row = dt.Rows[0];
                textBox1.Text = row["uid"].ToString();
                textBox2.Text = row["uname"].ToString();
                textBox3.Text = row["pwd"].ToString();
                textBox4.Text = row["uflag"].ToString();
            }
            if (label1.Text == "rid")
            {
                ds = SqlDesigner.ExecuteDataSet("select *from dtrole where rid='" + index + "'");
                dt = ds.Tables[0];
                DataRow row = dt.Rows[0];
                textBox1.Text = row["rid"].ToString();
                textBox2.Text = row["rname"].ToString();
                textBox3.Text = row["flag"].ToString();
                textBox4.Text = "None";
            }
            if (label1.Text == "fid")
            {
                ds = SqlDesigner.ExecuteDataSet("select *from dtfunction where fid='" + index + "'");
                dt = ds.Tables[0];
                DataRow row = dt.Rows[0];
                textBox1.Text = row["fid"].ToString();
                textBox2.Text = row["fname"].ToString();
                textBox3.Text = row["flag"].ToString();
                textBox4.Text = row["uflag"].ToString();
            }
        }
        private void labelshow() 
        {
            label1.Text = dataGridView1.Columns[0].HeaderText;
            label2.Text = dataGridView1.Columns[1].HeaderText;
            label3.Text = dataGridView1.Columns[2].HeaderText;
            try
            {
                label4.Text = dataGridView1.Columns[3].HeaderText;
            }
            catch (Exception)
            {

                label4.Text = "None";
            }                                                         
        }               
        private void btn_add_Click(object sender, EventArgs e)
        {            
            int i = 0;
            if (label1.Text=="uid")
            {
                string str = SqlDesigner.GetMD5(textBox3.Text.Trim());                
                i = SqlDesigner.ExecuteNoQuery("insert into dtuser(uid,uname,pwd,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + str + "','" + textBox4.Text + "')");
            }
            else if (label1.Text == "rid")
            {                
                i = SqlDesigner.ExecuteNoQuery("insert into dtrole(rid,rname,flag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')");
            }
            else
            {
                try
                {
                    i = SqlDesigner.ExecuteNoQuery("insert into dtfunction(fid,rid,uid,uflag)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
                }
                catch (Exception)
                {
                    MessageBox.Show("添加失敗");
                }
                
            }
            
            if (i > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失敗");
            }                                        
        }
        
        private void btn_del_Click(object sender, EventArgs e)
        {
            int i = 0;
            string currentIndex = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            if (label1.Text=="uid")
            {
                i = SqlDesigner.ExecuteNoQuery("delete from dtuser where uid='" + currentIndex + "'");
            }
            else if (label1.Text=="fid")
            {
                i = SqlDesigner.ExecuteNoQuery("delete from dtfunction where fid='" + currentIndex + "'");
            }
            else
            {
                i = SqlDesigner.ExecuteNoQuery("delete from dtrole where rid='" + currentIndex + "'");
            }
            if (i > 0)
            {
                MessageBox.Show("刪除成功");
            }
            else
            {
                MessageBox.Show("刪除失敗");
            }
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            int i = 0;
            if (label1.Text == "rid")
            {
                i = SqlDesigner.ExecuteNoQuery("update dtrole set rname='" + textBox2.Text + "',flag='" + textBox3.Text + "'where rid='" + textBox1.Text + "'");
            }
            if (label1.Text == "uid")
            {
                i = SqlDesigner.ExecuteNoQuery("update dtuser set uname='" + textBox2.Text + "',pwd='" + textBox3.Text + "',uflag='" + textBox4.Text + "'where uid='" + textBox1.Text + "'");
            }
            if (label1.Text=="fid")
            {
                i = SqlDesigner.ExecuteNoQuery("update dtfunction set rid='" + textBox2.Text + "',uid='" + textBox3.Text + "',uflag='" + textBox4.Text + "'where fid='" + textBox1.Text + "'");
            }
            if (i > 0)
            {
                MessageBox.Show("Succeed!");
            }
            else
            {
                MessageBox.Show("Failed!");
            }
        }

        

       
        
    }
}
復(fù)制代碼

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    亚洲免费黄色高清在线观看| 成人国产激情在线视频| 色欧美一区二区三区在线| 草草草草在线观看视频| 国产又色又爽又黄的精品视频| 国产免费一区二区不卡| 国产肥妇一区二区熟女精品| 久久99这里只精品热在线| 美女被啪的视频在线观看| 国产欧美日韩一级小黄片| 欧美午夜不卡在线观看| 欧美小黄片在线一级观看| 欧美成人久久久免费播放| 五月综合婷婷在线伊人| 久草视频在线视频在线观看| 亚洲天堂精品在线视频| 日韩欧美中文字幕人妻| 欧美日韩亚洲巨色人妻| 久久夜色精品国产高清不卡| 欧美一本在线免费观看| 麻豆果冻传媒一二三区| 蜜桃av人妻精品一区二区三区| 好吊视频有精品永久免费| 久久精品视频就在久久| 日木乱偷人妻中文字幕在线| 色婷婷激情五月天丁香| 老鸭窝精彩从这里蔓延| 欧美一级日韩中文字幕| 亚洲中文在线男人的天堂| 日韩精品亚洲精品国产精品| 色婷婷日本视频在线观看| 欧美午夜国产在线观看| 亚洲精品中文字幕无限乱码| 国产又粗又长又爽又猛的视频| 91麻豆视频国产一区二区| 日韩精品你懂的在线观看 | 日韩不卡一区二区视频| 人妻内射在线二区一区| 欧美日韩在线第一页日韩| 国产成人午夜在线视频| 中文字幕精品少妇人妻|