麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

WINFORM數據庫操作,有點像安裝里面的SQLITE

2024-07-21 02:52:54
字體:
來源:轉載
供稿:網友
程序設計要求

設計一個用戶管理系統,對系統中的用戶進行管理。假定,用戶表中有下列字段:用戶名,密碼,電話和 email 等信息。要求,1)利用 SQL server 首先創建用戶數據表;2)實現對用戶的增加、刪除、修改和查詢等功能。

2.程序設計流程圖

3.程序設計亮點

(0)根據學生在使用CMS系統和Discuz系統時,發現其數據庫的后臺操作界面很簡單,因此根據sql語句的基本組成,設置where條件選擇輸入區和更新插入的更新區。

(1)只有在勾選該字段時,才允許輸入該字段的值

(2)使用listview列出數據查詢結果

(3)支持數據瀏覽,無須手動輸入查詢語句

4.程序運行截圖

以下只是簡單的幾組測試,其他的sql組合同理能夠運行

(1) 瀏覽數據

注:選擇瀏覽操作的示意圖

(2)選擇查看符合相應條件的數據

注:設置選擇的條件示意圖

注:執行選擇后的結果

(3)插入數據

注:插入數據示意

注:數據插入后,瀏覽數據結果

(4)更新數據

注:設置更新條件示意圖

注:執行更新操作后

(5)刪除數據

注:設置刪除條件示意圖

注:刪除操作執行后

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data;using System.Data.Sql;using System.Data.SqlClient;namespace 數據庫操作_WNFORM{    public partial class Form1 : Form    {        PRivate SqlConnection sqlcon;        private SqlCommand sqlcom;        private SqlDataReader sda;        private string conn_str = @”Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|/info.mdf;Integrated Security=True;User Instance=True”;        public Form1()        {            InitializeComponent();        }         private void Form1_Load(object sender, EventArgs e)        {            listView_res.Columns.Add(“用戶名”);            listView_res.Columns.Add(“密  碼”);            listView_res.Columns.Add(“手機號”);            listView_res.Columns.Add(“郵箱號”);            listView_res.Columns[0].Width = 250;            listView_res.Columns[1].Width = 340;            listView_res.Columns[2].Width = 245;            listView_res.Columns[3].Width = 325;        }         private void checkBox1_CheckedChanged(object sender, EventArgs e)        {            textBox_name.Enabled = true;        }         private void checkBox3_CheckedChanged(object sender, EventArgs e)        {            textBox_pass.Enabled = true;        }         private void checkBox2_CheckedChanged(object sender, EventArgs e)        {            textBox_name2.Enabled = true;        }         private void checkBox6_CheckedChanged(object sender, EventArgs e)        {            textBox_pass2.Enabled = true;        }         private void checkBox4_CheckedChanged(object sender, EventArgs e)        {            textBox_tel.Enabled = true;        }         private void checkBox5_CheckedChanged(object sender, EventArgs e)        {            textBox_mail.Enabled = true;        }         private void checkBox7_CheckedChanged(object sender, EventArgs e)        {            textBox_tel2.Enabled = true;        }         private void checkBox8_CheckedChanged(object sender, EventArgs e)        {            textBox_mail2.Enabled = true;        }        private  void init_controler()        {            checkBox1.Checked = false;            checkBox2.Checked = false;            checkBox3.Checked = false;            checkBox4.Checked = false;            checkBox5.Checked = false;            checkBox6.Checked = false;            checkBox7.Checked = false;            checkBox8.Checked = false;                      textBox_mail.Text = “”;            textBox_name.Text = “”;            textBox_pass.Text = “”;            textBox_tel.Text = “”;            textBox_mail2.Text = “”;            textBox_name2.Text = “”;            textBox_pass2.Text = “”;            textBox_tel2.Text = “”;              textBox_mail.Enabled = false;            textBox_name.Enabled = false;            textBox_pass.Enabled = false;            textBox_tel.Enabled = false;            textBox_mail2.Enabled = false;            textBox_name2.Enabled = false;            textBox_pass2.Enabled = false;            textBox_tel2.Enabled = false;        }        private string [] get_where()        {            string [] res=new string[4];            if (textBox_name.Text != null)            {                 res[0]= textBox_name.Text.ToString();            }            if (textBox_pass.Text != null)            {                res[1]= textBox_pass.Text.ToString();            }            if (textBox_tel.Text != null)            {                res[2]= textBox_tel.Text.ToString();            }             if (textBox_mail.Text != null)            {                res[3]= textBox_mail.Text.ToString();            }            return res;        }        private string[] get_value()        {            string[] res = new string[4];            if (textBox_name2.Text != null)            {                res[0] = textBox_name2.Text.ToString();            }            if (textBox_pass2.Text != null)            {                res[1] = textBox_pass2.Text.ToString();            }            if (textBox_tel2.Text != null)            {                res[2] = textBox_tel2.Text.ToString();            }             if (textBox_mail2.Text != null)            {                res[3] = textBox_mail2.Text.ToString();            }            return res;        }        //執行數據庫查詢        private string execu_sql()        {             string res = “”;            sqlcon = new SqlConnection(conn_str);            sqlcom = new SqlCommand();            sqlcon.Open();            sqlcom.Connection = sqlcon;            string sw = “”;            sw = comboBox1.SelectedItem.ToString();            MessageBox.Show(sw);            /                瀏覽數據                查詢數據                插入數據                更新數據                刪除數據             /            switch (sw)            {                 case “瀏覽數據”:                    res = “select * from [用戶表]”;                    sqlcom.CommandText = res;                    int count = 0;                    try                    {                        sda = sqlcom.ExecuteReader();                        listView_res.Items.Clear();                        while (sda.Read())                        {                            ListViewItem lvi = new ListViewItem();                            lvi.Text = sda.GetString(0);                            lvi.SubItems.Add(sda.GetString(1));                            lvi.SubItems.Add(sda.GetString(2));                            lvi.SubItems.Add(sda.GetString(3));                            lvi.UseItemStyleForSubItems = false;                            lvi.SubItems[0].BackColor = Color.HotPink;                            lvi.SubItems[1].BackColor = Color.LightSkyBlue;                            lvi.SubItems[2].BackColor = Color.SpringGreen;                            lvi.SubItems[3].BackColor = Color.Orange;                            listView_res.Items.Add(lvi);                            count++;                        }                        if(count==0)                        {                            MessageBox.Show(“沒有查詢到匹配的數據”);                        }                    }                    catch (Exception ex)                    {                        MessageBox.Show(“出現數據查詢錯誤,請重試” + ex.Message);                    }                    finally {                        sqlcon.Close();                    }                    break;                case “查詢數據”:                    res = “select * from [用戶表]”;                    string [] wh=get_where();                    bool tags=false;                    if(wh!=null)                    {                        res+=” where “;                        if (wh[0]!= “”)                        {                            res += ” 用戶名=@user “;                            //res+=wh[0];                            tags=true;                        }                        if (wh[1] != “”)                        {                            if(tags)                                res+=”and “;                            res+=” 密碼=@pass “;                            //res+=wh[1];                            tags=true;                        }                        if (wh[2] != “”)                        {                            if(tags)                                res += ” and “;                            res+=” 電話=@tel “;                            //res+=wh[2];                            tags=true;                        }                        if(wh[3]!=”“)                        {                            if(tags)                                res += ” and “;                           res+=” 郵箱=@mail “;                           // res+=wh[1];                        }                        sqlcom.CommandText = res;                        if (wh != null)                        {                            if (wh[0] != “”)                            {                                sqlcom.Parameters.AddWithValue(“@user”, wh[0]);                            }                            if (wh[1] != “”)                            {                                sqlcom.Parameters.AddWithValue(“@pass”, wh[1]);                            }                            if (wh[2] != “”)                            {                                sqlcom.Parameters.AddWithValue(“@tel”, wh[2]);                            }                            if (wh[3] != “”)                            {                                sqlcom.Parameters.AddWithValue(“@mail”, wh[3]);                            }                        }                        try                        {                            sda = sqlcom.ExecuteReader();                            listView_res.Items.Clear();                            if (sda.Read())                            {                                ListViewItem lvi = new ListViewItem();                                lvi.Text = sda.GetString(0);                                lvi.SubItems.Add(sda.GetString(1));                                lvi.SubItems.Add(sda.GetString(2));                                lvi.SubItems.Add(sda.GetString(3));                                lvi.UseItemStyleForSubItems = false;                                lvi.SubItems[0].BackColor = Color.HotPink;                                lvi.SubItems[1].BackColor = Color.LightSkyBlue;                                lvi.SubItems[2].BackColor = Color.SpringGreen;                                lvi.SubItems[3].BackColor = Color.Orange;                                listView_res.Items.Add(lvi);                            }                            else                            {                                MessageBox.Show(“沒有查詢到匹配的數據,請檢查原因”);                            }                        }                        catch (Exception ex)                        {                            MessageBox.Show(“出現數據查詢錯誤,請重試” + ex.Message);                        }                        finally {                            sqlcon.Close();                        }                    }                    break;                case “插入數據”:                    res = “insert into [用戶表](“;                    string [] vl=get_value();                    bool tagi=false;                    if (vl != null)                    {                        if (vl[0] != “”)                        {                            res += “用戶名”;                            tagi = true;                        }                        if (vl[1] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”密碼 “;                            tagi = true;                        }                        if (vl[2] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”電話”;                            tagi = true;                        }                        if (vl[3] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”郵箱”;                        }                    }                    res += ” ) values(“;                    /*** 構造values      *******/                    tagi=false;                    if (vl != null)                    {                        if (vl[0] != “”)                        {                            res += “@user”;                            //res += vl[0];                            tagi = true;                        }                        if (vl[1] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”@pass”;                            //res += vl[1];                            tagi = true;                        }                        if (vl[2] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”@tel”;                           // res += vl[2];                            tagi = true;                        }                        if (vl[3] != “”)                        {                            if (tagi)                                res+=” , “;                            res+=”@mail”;                            //res += vl[1];                        }                    }                    res += ” )”;                    sqlcom.CommandText = res;                    if (vl != null)                    {                        if (vl[0] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@user”, vl[0]);                        }                        if (vl[1] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@pass”, vl[1]);                        }                        if (vl[2] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@tel”, vl[2]);                        }                        if (vl[3] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@mail”, vl[3]);                        }                    }                    int i = sqlcom.ExecuteNonQuery();                    if (i > 0)                    {                        MessageBox.Show(“插入成功,請瀏覽數據”);                    }                    else {                        MessageBox.Show(“插入失敗,請檢查原因”);                    }                    sqlcon.Close();                    break;                case “更新數據”:                    res = “update [用戶表] set”;                    string [] vlu=get_value();                    string[] whu = get_where();                    bool tagu=false;                    if (vlu != null)                    {                        if (vlu[0] != “”)                        {                            res += ” 用戶名=”;                            res += ” @user2”;                            tagu = true;                        }                        if (vlu[1] != “”)                        {                            if (tagu)                                res+=” , “;                            res+=” 密碼=”;                            res += ” @pass2”;                            tagu = true;                        }                        if (vlu[2] != “”)                        {                            if (tagu)                                res+=” , “;                            res+=” 電話=”;                            res += ” @tel2”;                            tagu = true;                        }                        if (vlu[3] != “”)                        {                            if (tagu)                                res+=” , “;                            res+=” 郵箱=”;                            res+=” @mail2”;                        }                     }                    tagu = false;                    if (whu != null)                    {                        res+=” where “;                        if (whu[0] != “”)                        {                            res += ” 用戶名=@user “;                            //res+=wh[0];                            tagu = true;                        }                        if (whu[1] != “”)                        {                            if (tagu)                                res+=”and “;                            res+=” 密碼=@pass “;                            //res+=wh[1];                            tagu = true;                        }                        if (whu[2] != “”)                        {                            if (tagu)                                res += ” and “;                            res+=” 電話=@tel “;                            //res+=wh[2];                            tagu = true;                        }                        if (whu[3] != “”)                        {                            if (tagu)                                res += ” and “;                           res+=” 郵箱=@mail “;                           // res+=wh[1];                        }                    }                     /                                           *                      *                      */                                    sqlcom.CommandText = res;                    if (vlu != null)                    {                        if (vlu[0] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@user2”, vlu[0]);                        }                        if (vlu[1] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@pass2”, vlu[1]);                        }                        if (vlu[2] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@tel2”, vlu[2]);                        }                        if (vlu[3] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@mail2”, vlu[3]);                        }                    }                    if (whu != null)                    {                        if (whu[0] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@user”, whu[0]);                        }                        if (whu[1] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@pass”, whu[1]);                        }                        if (whu[2] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@tel”, whu[2]);                        }                        if (whu[3] != “”)                        {                            sqlcom.Parameters.AddWithValue(“@mail”, whu[3]);                        }                    }                    int pcount = sqlcom.ExecuteNonQuery();                    if (pcount > 0)                    {                        MessageBox.Show(“更新成功,請瀏覽數據”);                    }                    else {                        MessageBox.Show(“更新失敗,請檢查原因”);                    }                    sqlcon.Close();                    break;                case “刪除數據”:                     res = “delete  from [用戶表]”;                    string [] whd=get_where();                    bool tagd=false;                    if (whd != null)                    {                        res+=” where “;                        if (whd[0] != “”)                        {                            res += ” 用戶名=@user “;                            //res+=wh[0];                            tagd = true;                        }                        if (whd[1] != “”)                        {                            if (tagd)                                res+=”and “;                            res+=” 密碼=@pass “;                            //res+=wh[1];                            tagd = true;                        }                        if (whd[2] != “”)                        {                            if (tagd)                                res += ” and “;                            res+=” 電話=@tel “;                            //res+=wh[2];                            tagd = true;                        }                        if (whd[3] != “”)                        {                            if (tagd)                                res += ” and “;                           res+=” 郵箱=@mail “;                           // res+=wh[1];                        }                        sqlcom.CommandText = res;                        if (whd != null)                        {                            if (whd[0] != “”)                            {                                sqlcom.Parameters.AddWithValue(“@user”, whd[0]);                            }                            if (whd[1] != null)                            {                                sqlcom.Parameters.AddWithValue(“@pass”, whd[1]);                            }                            if (whd[2] != null)                            {                                sqlcom.Parameters.AddWithValue(“@tel”, whd[2]);                            }                            if (whd[3] != null)                            {                                sqlcom.Parameters.AddWithValue(“@mail”, whd[3]);                            }                        }                        try                        {                            int rc=sqlcom.ExecuteNonQuery();                            if (rc > 0)                            {                                MessageBox.Show(“數據刪除成功,請瀏覽數據”);                            }                            else                            {                                MessageBox.Show(“數據刪除失敗,請檢查原因”);                            }                        }                        catch (Exception ex)                        {                            MessageBox.Show(“出現數據查詢錯誤,請重試” + ex.Message);                        }                        finally {                            sqlcon.Close();                        }                    }                    break;            }            return res;        }        private void button1_Click(object sender, EventArgs e)        {            init_controler();                    }         private void button2_Click(object sender, EventArgs e)        {            if (comboBox1.SelectedItem == null)            {                MessageBox.Show(“請選擇右上方的操作后再執行確認”);                return;            }            execu_sql();            init_controler();        }    }} 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久在草 | 日韩精品a在线观看 | 视频一区二区精品 | 久久久一区二区 | 欧美高清第一页 | 国产91精品久久久久久 | lutube成人福利在线观看 | 久久久一区二区精品 | 亚洲第一综合色 | porno video hd 365hd | 色妞视频男女视频 | 欧美成人精品一级 | 色网站免费观看 | 午夜a狂野欧美一区二区 | 成人免费自拍视频 | www.99热精品 | 毛片一级视频 | 在线播放免费播放av片 | 黄视频免费在线观看 | 播色网 | 日日噜噜夜夜爽 | 国产精品午夜未成人免费观看 | 正在播放91 | 狠狠干夜夜草 | 免费毛片小视频 | 国产精品视频免费网站 | 亚洲成人网一区 | 亚洲aⅴ免费在线观看 | 91在线视频在线观看 | 亚洲视频在线观看免费 | 国产免费午夜 | 高清av免费| 高清一区二区在线观看 | 原来神马影院手机版免费 | 日本aaaa片毛片免费观蜜桃 | 91麻豆精品国产91久久久点播时间 | 免费看毛片网站 | 黑人三级毛片 | 日本欧美一区二区三区在线观看 | 超碰人人做人人爱 | www.com香蕉 |