C#在winform中實現(xiàn)數(shù)據(jù)增刪改查等功能
winform中利用ado.net實現(xiàn)對單表的增刪改查的詳細例子,具體如下:
1.前言:
運行環(huán)境:VS2013+SQL2008+Windows10
程序界面預覽:

使用的主要控件:dataGridview和menuStrip等。
2.功能具體介紹:
1.首先,我們要先實現(xiàn)基本的數(shù)據(jù)操作,增刪改查這幾個操作。
(1)先定義一個數(shù)據(jù)庫操作的公共類:
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));
//翻轉生成的MD5碼
s.Reverse();
//通過使用循環(huán),將字節(jié)類型的數(shù)組轉換為字符串,此字符串是常規(guī)字符格式化所得
//只取MD5碼的一部分,這樣惡意訪問者無法知道取的是哪幾位
for (int i = 3; i < s.Length - 1; i++)
{
//將得到的字符串使用十六進制類型格式。格式后的字符是小寫的字母,如果使用大寫(X)則格式后的字符是大寫字符
//進一步對生成的MD5碼做一些改造
pwd = pwd + (s[i] < 198 ? s[i] + 28 : s[i]).ToString("X");
}
return pwd;
}
}
}
(2)運用建立的公共類,進行數(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.一些細節(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; //自動適應列寬 dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
3.代碼僅供參考:
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!");
}
}
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Unity的IPreprocessBuild實用案例深入解析
這篇文章主要為大家介紹了Unity的IPreprocessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
C#版免費離線人臉識別之虹軟ArcSoft?V3.0(推薦)
本文只是簡單介紹了如何使用虹軟的離線SDK,進行人臉識別的方法,并且是圖片的方式,本地離線識別最大的好處就是沒有延遲,識別結果立馬呈現(xiàn),對C#離線人臉識別虹軟相關知識感興趣的朋友一起看看吧2021-12-12
C# MVC 使用LayUI實現(xiàn)下拉框二級聯(lián)動的功能
這篇文章主要介紹了C# MVC 如何使用LayUI實現(xiàn)下拉框二級聯(lián)動,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下2020-06-06
C#實現(xiàn)的文件操作封裝類完整實例【刪除,移動,復制,重命名】
這篇文章主要介紹了C#實現(xiàn)的文件操作封裝類,結合完整實例形式分析了C#封裝文件的刪除,移動,復制,重命名等操作相關實現(xiàn)技巧,需要的朋友可以參考下2017-03-03

