如何从窗口表单应用程序发送值到asp.net服务器,并从那里获取值表单数据库

本文关键字:表单 服务器 net 从那里 数据库 获取 asp 窗口 应用程序 | 更新日期: 2023-09-27 17:49:23

我想让用户获得用户名和密码在窗口形式发送到一个web表单进行验证,其中从数据库验证后的条目(这是由用户联合的课程)被发送回窗口形式,我搜索了很多,但无法得到任何合适的方法,我是c#和asp.net的新手。我在这里找到了一种方法,我把我的代码请告诉我是否有更好的方法来做我的工作。但是,而不是得到所需的值,我得到整个html代码,所以我曾经把我想要的值在html标签在服务器端,在客户端,我通过使用正则表达式得到的值下面是windows的代码,从文本框

获取值后发送
 try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://localhost:50612/Inclusive_LMS/speech/login.aspx/?stdId=" +tb_stdId.Text + "&pass=" + tb_pass.Text);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string content = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Match valid = Regex.Match(content, @"(?<=(<span[^>]*>))([^<]*)(?=(</span>))");
            if (valid.Success)
            {
                this.Hide();
                Student_dashboard ins = new Student_dashboard();
                ins.Show();
            }
            else
            {
                MessageBox.Show("invalid user Id or password");
            }

           // textBox2.Text = "" + content + "";
        }
        catch (Exception ex)
        {
            MessageBox.Show("check your internet connection, failed to connect the server.");
        }

下面是"http://localhost:50612/Inclusive_LMS/speech/login"的代码。在设计端,我在html页面上只有一个标签

    SqlDataReader r;
    SqlCommand cmd = new SqlCommand("Select * from Student where Std_Id='" +Convert.ToInt32(Request.QueryString["stdId"]) + "' and Password='" +Request.QueryString["pass"]+ "'", con);
   try
   {
    con.Open();
        r = cmd.ExecuteReader();
        if (r.Read())
            Label1.Text = "success";
        else
            Label1.Text = "fail";
        con.Close();
     }
      catch
      {
          Label1.Text = "error";
      }

如何从窗口表单应用程序发送值到asp.net服务器,并从那里获取值表单数据库

我想,对于任何任务,最好的方法是直接从windows应用程序(如果可能的话)访问数据库。

如果不能直接访问数据库,可以在webform项目中创建一个web服务,并在winforms应用程序中使用它。这很容易实现并且运行良好。

第三种方法是在ASP中使用WebAPI。. NET框架来执行此任务。

你需要选择最适合你的场景。祝你好运!

更新:访问ASP。. NET官方网站的大量资源。