Winform简单的增删改

DbHelper.cs//简单的数据库连接
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace SimpleTest
{
   public class DbHelper
    { 
       public static string strCon = "server=.;database=UserInfo;integrated security=true";
       public static SqlConnection con = new SqlConnection(strCon);
    }
}
Form1.cs
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.SqlClient;


namespace SimpleTest
{
    public partial class Form1 : Form
    {
        
        //int index;全局变量可以设置使用也可不使用凭自己喜好
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetData();
        }
        //绑定数据方法
        public void GetData()
        {
            string sql = "select * from UserInfo";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql,DbHelper.con);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        //添加事件
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "insert into UserInfo values(@name,@pwd)";
            SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
            cmd.Parameters.AddWithValue("@name", textBox1.Text);
            cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
            try
            {
                DbHelper.con.Open();
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("添加成功!");
                    GetData();
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                else
                {
                    MessageBox.Show("添加失败!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DbHelper.con.Close();
            }
           
        }
        //单击单元格内容
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            //index = e.RowIndex;
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            //textBox1.Text = dataGridView1.Rows[index].Cells[1].Value.ToString();
            //textBox2.Text = dataGridView1.Rows[index].Cells[2].Value.ToString();
        }

        //修改事件
        private void button2_Click(object sender, EventArgs e)
        {
                string sql = "update UserInfo set LoginName=@name, LoginPwd=@pwd where UserId=@id";
                SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
                cmd.Parameters.AddWithValue("@id", int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
                cmd.Parameters.AddWithValue("@name", textBox1.Text);
                cmd.Parameters.AddWithValue("@pwd", textBox2.Text);
                try
                {
                    DbHelper.con.Open();
                    int result = cmd.ExecuteNonQuery();
                    if (result > 0)
                    {
                        MessageBox.Show("修改成功!");
                        GetData();
                        textBox1.Text = "";
                        textBox2.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("修改失败!");
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    DbHelper.con.Close();
                }
       
          
        }
        //删除事件
        private void button3_Click(object sender, EventArgs e)
        {
            //string sql = "delete UserInfo  where userid= " + int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            string sql = "delete UserInfo  where userid=@id ";
            SqlCommand cmd = new SqlCommand(sql, DbHelper.con);
            cmd.Parameters.AddWithValue("@id", int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
            try
            {
                DbHelper.con.Open();
                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("删除成功!");
                    GetData();
                    textBox1.Text = "";
                    textBox2.Text = "";
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DbHelper.con.Close();
           }
        }
        //退出事件
        private void button4_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }      

    }
}
设置的界面如下:

运行效果如下:








退出直接退出应用程序


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我提供以下代码示例,实现一个基于 Socket 通信的字典增删功能: 首先,需要先建立一个服务器端和客户端的连接,可以使用以下代码: 服务器端: ```csharp // 创建一个TcpListener对象,监听指定的IP地址和端口号 TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080); // 开始监听 tcpListener.Start(); // 等待客户端连接 TcpClient tcpClient = tcpListener.AcceptTcpClient(); // 获取网络流对象 NetworkStream networkStream = tcpClient.GetStream(); ``` 客户端: ```csharp // 创建一个TcpClient对象,连接指定的服务器IP地址和端口号 TcpClient tcpClient = new TcpClient("127.0.0.1", 8080); // 获取网络流对象 NetworkStream networkStream = tcpClient.GetStream(); ``` 接下来,在服务器端和客户端之间发送数据,可以使用以下代码: 服务器端: ```csharp // 向客户端发送数据 byte[] sendData = Encoding.UTF8.GetBytes("Hello, client!"); networkStream.Write(sendData, 0, sendData.Length); ``` 客户端: ```csharp // 从服务器端接收数据 byte[] receiveData = new byte[1024]; int length = networkStream.Read(receiveData, 0, receiveData.Length); string receiveMsg = Encoding.UTF8.GetString(receiveData, 0, length); ``` 以上是建立连接和发送数据的基本操作,接下来我们来实现字典的增删功能。我们可以定义一个字典变量,然后通过 Socket 通信的方式来对字典进行增删操作。 以下是一个简单的代码示例: ```csharp // 定义一个字典变量 Dictionary<string, string> dict = new Dictionary<string, string>(); // 接收客户端发来的数据 byte[] receiveData = new byte[1024]; int length = networkStream.Read(receiveData, 0, receiveData.Length); string receiveMsg = Encoding.UTF8.GetString(receiveData, 0, length); // 解析客户端发来的请求 string[] request = receiveMsg.Split('|'); string action = request[0]; string key = request[1]; // 根据请求操作字典 switch (action) { case "add": string value = request[2]; if (dict.ContainsKey(key)) { dict[key] = value; } else { dict.Add(key, value); } break; case "delete": if (dict.ContainsKey(key)) { dict.Remove(key); } break; case "update": string newValue = request[2]; if (dict.ContainsKey(key)) { dict[key] = newValue; } break; default: break; } // 向客户端发送操作后的字典数据 string dictStr = ""; foreach (var item in dict) { dictStr += item.Key + ":" + item.Value + "|"; } byte[] sendData = Encoding.UTF8.GetBytes(dictStr); networkStream.Write(sendData, 0, sendData.Length); ``` 以上代码实现了一个简单的字典增删的功能,具体实现需要根据实际需求进行修改
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值