WPF操作SQL SERVER 数据库实例讲解

wpf操作sql server 实例讲解

using system; 
using system.collections.generic; 
using system.data.sqlclient; 
using system.linq; 
using system.text; 
using system.threading.tasks; 
using system.windows; 
using system.windows.controls; 
using system.windows.data; 
using system.windows.documents; 
using system.windows.input; 
using system.windows.media; 
using system.windows.media.imaging; 
using system.windows.navigation; 
using system.windows.shapes;

namespace demo_sql 
{ 
/// 
/// mainwindow.xaml 的交互逻辑 
/// 
public partial class mainwindow : window 
{ 
public mainwindow() 
{ 
initializecomponent(); 
windowstartuplocation = windowstartuplocation.centerscreen; 
}
    private void add_click(object sender, routedeventargs e)
    {
        string strconn = "server=(localdb)\\mssqllocaldb;database=studentdb;integrated security=true";
        sqlconnection sqlconn = new sqlconnection(strconn);
        try
        {
            sqlconn.open();
            //messagebox.show("连接数据库成功");
            string sqladd = "insert into student(name, password) values ('" +name.text + "', '" + password.text + "')";
            sqlcommand sqlcmd = new sqlcommand(sqladd, sqlconn);
            sqlcmd.executenonquery();
            messagebox.show("插入成功");
        }
        catch (exception ex)
        {
            messagebox.show("数据库打开失败,详细信息:" + ex.tostring());
        }
        finally
        {
            sqlconn.close();
        }
    }

    private void modify_click(object sender, routedeventargs e)
    {
        string strconn = "server=(localdb)\\mssqllocaldb;database=studentdb;integrated security=true";
        sqlconnection sqlconn = new sqlconnection(strconn);
        try
        {
            sqlconn.open();
            string strmodify = "update student set password='" + password.text + "'" + " where name=" + "'" + name.text + "'";
            sqlcommand sqlcmd = new sqlcommand(strmodify, sqlconn);
            sqlcmd.executenonquery();
            messagebox.show("修改成功");
        }
        catch (exception ex)
        {
            messagebox.show("连接错误" + ex.message);
        }
        finally
        {
            sqlconn.close();
        }
    }

    private void delete_click(object sender, routedeventargs e)
    {
        string strconn = "server=(localdb)\\mssqllocaldb;database=studentdb;integrated security=true";
        sqlconnection sqlconn = new sqlconnection(strconn);
        try
        {
            sqlconn.open();
            string strdelete = "delete from student where name='" + name.text + "'";
            sqlcommand sqlcmd = new sqlcommand(strdelete, sqlconn);
            sqlcmd.executenonquery();
            messagebox.show("删除成功");
        }
        catch (exception ex)
        {
            messagebox.show("连接错误"+ex.message);
        }
        finally
        {
            sqlconn.close();
        }
    }

    private void search_click(object sender, routedeventargs e)
    {
        int flag = 1;
        string strconn = "server=(localdb)\\mssqllocaldb;database=studentdb;integrated security=true";
        sqlconnection sqlconn = new sqlconnection(strconn);
        try
        {
            sqlconn.open();
            //messagebox.show("连接数据库成功");
            string sqlsearch = "select * from student where name='" + name.text + "'";
            sqlcommand sqlcmd = new sqlcommand(sqlsearch, sqlconn);
            sqldatareader reader = sqlcmd.executereader();
            //读取数据 
            while (reader.read())
            {
                // 可以使用数据库中的字段名,也可以使用角标访问
                if(reader["password"].tostring()==password.text )
                {
                    flag = 0;
                    break;
                }
            }
            if(flag == 1)
                messagebox.show("用户不存在");
            else
                messagebox.show("存在用户");
        }
        catch (exception ex)
        {
            messagebox.show("数据库打开失败,详细信息:" + ex.tostring());
        }
        finally
        {
            sqlconn.close();
        }
    }
}

}

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐