在C#中通过命令对象执行SQL

本文介绍了一个使用C#进行数据库操作的简单示例,包括插入、更新、删除及查询等基本操作。通过实例展示了如何连接数据库、执行SQL语句,并处理结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class frmSql : Form
    {
        //两个文本框,名为txtUid,txtPwd;一个组合框,名为cboUser
        //四个按钮,名为btnInsert,btnDelete,btnUpdate,btnSelect        
        //数据库名为testSql,表结构为:Users(Id, Username, Password)
        public frmSql()
        {
            InitializeComponent();
        }

        string cnnStr =@"integrated security=true;database=testProcedure;server=.\sqlexpress";

        private void frmSql_Load(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(cnnStr);
            cnn.Open();

            SqlCommand cmd = new SqlCommand("select * from users", cnn);

            SqlDataReader dr = cmd.ExecuteReader();

            string userName = ""; 
      
            while (dr.Read()) //添加用户名到组合框中
            {
                userName = dr[1].ToString();
                cboUser.Items.Add(userName);
            }
            dr.Close();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {//按用户名模糊查找
            SqlConnection cnn = new SqlConnection(cnnStr);
            cnn.Open();

            string strSql = string.Format("select * from users where username like '%{0}%'", txtUid.Text);
            SqlCommand cmd = new SqlCommand(strSql, cnn);

            SqlDataReader dr = cmd.ExecuteReader();

            dr.Read();

            if (dr.HasRows == true)
            {
                string userName = dr[1].ToString();
                MessageBox.Show(userName + " 找着了!");
            }
            else
            {
                MessageBox.Show(txtUid.Text + " 用户不存在!");
            }

            dr.Close();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {//新增用户
            SqlConnection cnn = new SqlConnection(cnnStr);
            cnn.Open();

            if (txtUid.Text == "" || txtPwd.Text == "")
            {
                MessageBox.Show("用户名和密码不能为空!");
                return;
            }

            string strSql = string.Format("insert into users(username,password) values('{0}','{1}')", 
                txtUid.Text,txtPwd.Text);
            SqlCommand cmd = new SqlCommand(strSql, cnn);

            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("新增用户成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {//按用户名修改密码
            SqlConnection cnn = new SqlConnection(cnnStr);
            cnn.Open();

            string strSql = string.Format("select count(*) from users where username ='{0}'", txtUid.Text);
            SqlCommand cmd = new SqlCommand(strSql, cnn);

            int res=Convert.ToInt32(cmd.ExecuteScalar());

            if (res == 0)
            {
                MessageBox.Show("用户不存在!");
                return;
            }
            
            strSql = string.Format("update users set password='{1}' where username='{0}'", 
                txtUid.Text, txtPwd.Text);
            cmd.CommandText = strSql;

            try
            {
                if (cmd.ExecuteNonQuery()>0)
                    MessageBox.Show("修改用户密码成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {//按用户名删除用户
            SqlConnection cnn = new SqlConnection(cnnStr);
            cnn.Open();

            string strSql = string.Format("delete from users where username='{0}'", txtUid.Text);
            SqlCommand cmd = new SqlCommand(strSql, cnn);

            try
            {
                if (cmd.ExecuteNonQuery()>0)
                    MessageBox.Show("删除用户成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                cnn.Close();
            }
        }        
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值