简体   繁体   English

如何将值从窗体窗体应用发送到asp.net服务器并从中获取值窗体数据库?

[英]how to send values form the window form appliction to an asp.net server and get the values form database from there?

I want user to get the user name and password in the windows form the send it to a web form for the verification, where after verification from the database the entries(which are courses joint by the user) are sent back to the window form, I searched alot but could not get any suitable method, I am new ti c# and asp.net.. I found a way here I am placing my code please tell me is there any better way to do my job.but instead of getting the desired values i get the whole html code so i used to enclose my desired value in html tags on server side which and on client side I am getting the values by using the regular expressions Here is code for windows from which sends after getting values from the text boxes 我希望用户在Windows窗体中获取用户名和密码,然后将其发送到Web窗体进行验证,然后在从数据库中进行验证之后,将条目(由用户联合完成的课程)发送回窗口窗体,我搜索了很多,但是找不到任何合适的方法,我是新的ti 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.");

        }

Here is the code for " http://localhost:50612/Inclusive_LMS/speech/login.aspx " page,on designer side I had only one lable on html page 这是“ http:// localhost:50612 / Inclusive_LMS / speech / login.aspx ”页面的代码,在设计者方面,我在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";
      }

I guess, the best approach would be to access the database directly from the windows application (If it is possible) for any task. 我猜,最好的方法是直接从Windows应用程序(如果可能)访问数据库以执行任何任务。

If it is not possible for you to access database directly, you can create a web service in your webform project and consume it in the winforms application. 如果无法直接访问数据库,则可以在webform项目中创建一个Web服务,然后在winforms应用程序中使用它。 This is easy to implement and works well. 这很容易实现并且效果很好。

The third approach is to use WebAPI in ASP.NET framework for doing this task. 第三种方法是使用ASP.NET框架中的WebAPI来完成此任务。

You need to choose what fits best in your scenario. 您需要选择最适合您的方案。 Best of luck! 祝你好运!

Update: Visit ASP.NET Official Website for lots of resources. 更新:访问ASP.NET官方网站以获取大量资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM