功能实现SQL命令的增删改;实现SQL的存储过程的增删改;

目的:熟悉WPF与SQL连接的操作;

     掌握SQL命令的增删改的方式;

     掌握存储过程增删改的方式;

     比较两种方式的不同。

效果截图:SQL命令增删改界面---WPF字符串SQL命令的增删改与存储过程的增删改

 

存储过程增删改界面:

WPF字符串SQL命令的增删改与存储过程的增删改

 

2.步骤和思路

先测试Sql命令的增删改(新建一个类Test.cs------MainWindow.xaml前台设置---------MainWindow.xaml.cs后台设置

 

再测试存储过程的增删改(新建一个类proc.cs----------新建一个窗口TestProc.xaml,前台编辑--------TestProc.xaml.cs后台编辑)

 

然后再主界面放置一个按钮,跳转到存储过程增删改的窗口。

 

3.Sql命令增删改代码

 Test.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;//
using System.Data.SqlClient;
using System.Windows;
using System.Data;//

namespace WpfDataGridAdoTest
{  
  

    public class DBSimple
    {



        private SqlConnection con;

        public DBSimple()
        {
            string str = @"Data Source=PC01;Integrated Security=SSPI;database=SuperMarket";
            con = new SqlConnection(str);

        }

        public void TestExecuteNonQuery_Insert(string id, string name)
        {
            if (con == null) return;
            string sql = "insert 员工信息 values('" + id + "','" + name + "')";
            SqlCommand cmd = new SqlCommand(sql, con);
            if (con.State == ConnectionState.Closed)
                con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void TestExecuteNonQuery_Delete(string id)
        {
            if (con == null) return;
            string sql = "delete from 员工信息 where 员工ID='" + id + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            if (con.State == ConnectionState.Closed)
                con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void TestExecuteNonQuery_Update(string id, string name)
        {
            if (con == null) return;
            string sql = "update 员工信息 set 员工姓名='" + name + "' where 员工ID='" + id + "'";
            SqlCommand cmd = new SqlCommand(sql, con);
            if (con.State == ConnectionState.Closed)
                con.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-31
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2022-12-23
  • 2022-01-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-21
  • 2022-01-18
  • 2021-12-20
  • 2021-11-28
相关资源
相似解决方案