设为首页 加入收藏

TOP

C#利用WinForm调用WebServices实现增删改查(二)
2017-10-13 10:42:29 】 浏览:3165
Tags:利用 WinForm 调用 WebServices 实现 删改
0
public bool update(string UserID, string UserName, string UserSex, string UserAge, string UserPassword, string UserType, ref string errormessage) 101 { 102 string constr = "Data Source=.;Initial Catalog=DB_UserSystem;User ID=sa;Password=123456"; 103 using (SqlConnection con = new SqlConnection(constr)) 104 { 105 string sql = "update TB_UserInfo set UserName='"+UserName+"',UserSex='"+UserSex+"',UserAge='"+UserAge+"',UserPassword='"+UserPassword+"',UserType='"+UserType+"' where UserID='"+UserID+"'"; 106 using (SqlCommand cmd = new SqlCommand(sql, con)) 107 { 108 try 109 { 110 con.Open(); 111 cmd.ExecuteNonQuery(); 112 con.Close(); 113 return true; 114 } 115 catch (Exception ex) 116 { 117 errormessage = ex.Message.ToString(); 118 return false; 119 } 120 } 121 122 } 123 } 124 } 125

2-->因为我的WinForm已经做好了,所以接下来就在此基础上进行操作。

界面如下:

3-->我们首先要引用Web服务的地址,我们找到刚才发布Web服务的地址,http://localhost:1666/Infos.asmx,由于我是直接在我以前做的基础上做的,上一个Web服务是新建的,里面方法什么的也没有写,所以我就贴了我做好了Web地址。

这个时候找到WinForm所在的项目,右击-->添加-->服务引用

地址输入我们之前发布WebServices的地址,由于我已经添加引用,这里我就不再折腾一遍了。

4-->由于我们要调用WebServices,所以我们先要将它实例化。

代码如下:(仅供参考)

由于我也是今天刚在老师的指导下完成,对于一些代码也是一知半解。所以这里就直接贴上来,方便明天继续思考和练习。

namespace WindowsFormsApplication14
{
    public partial class Form1 : Form
    {

        private ServiceReference.InfosSoapClient ws;  //这样就省得在每一个方法中都实例化了
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“dB_UserSystemDataSet.TB_UserInfo”中。您可以根据需要移动或删除它。
            //this.tB_UserInfoTableAdapter.Fill(this.dB_UserSystemDataSet.TB_UserInfo);
            //LoadData();
            ws = new ServiceReference.InfosSoapClient();  //同上
        }

      
        private void button1_Click(object sender, EventArgs e)
        {

            string UserID = txtUserID.Text.Trim();
            string UserName = txtUserName.Text.Trim();
            string UserSex = this.comboBox2.Text;
            string UserAge = txtUserAge.Text;
            string UserPassword = txtUserPassword.Text;
            string UserType = this.comboBox3.Text;

            string errorinfo = "";

            if (ws.add(txtUserID.Text, txtUserName.Text, this.comboBox2.Text, txtUserAge.Text, txtUserPassword.Text, this.comboBox3.Text, ref errorinfo))
            {
                MessageBox.Show("注册成功" + errorinfo);
            }
            else
            {
                MessageBox.Show("注册失败" + errorinfo);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int UserAge=0;

            try
            {
                int.Parse(txtUserAge.Text);
                UserAge = Convert.ToInt32(txtUserAge.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入不正确", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                return;
            }

            string errorinfo = "";
            if (ws.update(txtUserID.Text,txtUserName.Text,this.comboBox2.Text,txtUserAge.Text,txtUserPassword.Text,this.comboBox3.Text,ref errorinfo))
            {
                MessageBox.Show("更新成功" );
            }
            else
            {
                MessageBox.Show("更新失败" );
            }           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要删除吗?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                string errorinfo = "";
                if (ws.delete(txtUserID.Text, ref errorinfo))
                {
                    MessageBox.Show("删除成功" + er
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇如何在docker配置asp.net core ht.. 下一篇C#函数式编程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目