c#winform定时上传数据到web网页并显示

//winform端(传输端方式)

建一个winform项目,添加一个定时器控件,一个textbox控件,在后台添加如下代码

//用到的空间名
using System.Diagnostics;//打开网址
using System.Net;
using System.IO;

 private void Main_Form_Load(object sender, EventArgs e)
        {

            webtimer.Enabled = true;
          
        }
//定义一个方法
       public static string Post(string url, string content)
        {
            string result = "";//声明一个容器result接收数据           
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);//创建一个HttpWebRequest,申明传输方式POST            
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";//获取或设置内容类型HTTP标头的值

                byte[] data = Encoding.UTF8.GetBytes(content);//添加POST参数
                req.ContentLength = data.Length;//获取或设置内容长度HTTP标头的值
                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(data, 0, data.Length);
                    reqStream.Close();
                }

                //申请一个容器resp接收返回数据
                 HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                 Stream stream = resp.GetResponseStream();
                //获取响应内容
                using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                {
                    result = reader.ReadLine ();
                }             
            }
            catch (Exception)
            {
              
            }
            return result;

        }
        /// <summary>
        /// 定时发送数据到网站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void webtimer_Tick(object sender, EventArgs e)
        {
            //调用数据发布到网站事件并设置上传内容
           string result = Post("http://localhost:28207/WebService/SaveFileWebForm.aspx", Main_Form.Instance.ToolStripStatusLabel1.Text);
            Console.WriteLine(result);
            textBox1.Text = result;
        }

//web网页接收端

1.创建web应用程序

2.新建一个文件夹

 

3.在新建文件夹中添加web窗体

4.项目名称自己取

 5.创建后文件夹下文件如下

 

6.在前台里添加 创建一个textbox,设置一个ID这样可以在后台登录程序中赋值给控件,runat=“server”,

7.登录后将网址赋值到c#winform代码里定时器事件变量里

8.启动web端程序,启动c#端程序,结果如下

 

9.发现信息显示不全,点击拆分,调节控件大小,重新编译即可

  

//后台程序
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SaveFileWebService.WebService
{
   
    public partial class SaveFileWebForm : System.Web.UI.Page
    {

         static  string result = "";
        string jsonStr = "", line;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            this.texpathend.Value = result;//为前台texpathend控件内容赋值

               if (Request.TotalBytes  > 0)
               {
                   try
                   {

                       Stream streamResponse = Request.InputStream;
                       StreamReader streamRead = new StreamReader(streamResponse, Encoding.UTF8);

                       while ((line = streamRead.ReadLine()) != null)
                       {
                           jsonStr += line;
                       }
                       streamResponse.Close();
                       streamRead.Close();

                       result = jsonStr;
                    
                       Response.Write(result);
                   }
                   catch
                   {
                       result = "msg-数据发布(In)异常:";
                       Response.Write(result);
                   }
               }
               else
               {
                   Response.Write("Error1");
               } 
        }
    }
}

//前台程序
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SaveFileWebForm.aspx.cs" Inherits="SaveFileWebService.WebService.SaveFileWebForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        #texpathend {
            width: 879px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div  > 
       <input name="end_1" type ="text" id="texpathend" runat ="server" value ="" clientidmode="Static"/>
    </div>
        
    </form>
</body>
</html>

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C# WinForm中将Excel数据导入到SQL Server数据库中的步骤: 1. 需要添加对Microsoft.Office.Interop.Excel和System.Data.SqlClient的引用。 2. 建立一个Windows Form应用程序,并在表单上添加一个按钮,用于触发导入Excel数据的过程。 3. 在按钮的Click事件中编写代码,打开Excel文件并读取数据。 ```csharp using Excel = Microsoft.Office.Interop.Excel; Excel.Application excelApp = new Excel.Application(); Excel.Workbook workbook = excelApp.Workbooks.Open(@"C:\example.xlsx"); Excel.Worksheet worksheet = workbook.Sheets[1]; int rowsCount = worksheet.UsedRange.Rows.Count; int columnsCount = worksheet.UsedRange.Columns.Count; for (int row = 1; row <= rowsCount; row++) { for (int col = 1; col <= columnsCount; col++) { string cellValue = (worksheet.Cells[row, col] as Excel.Range).Value.ToString(); // Do something with the cell value } } workbook.Close(); excelApp.Quit(); ``` 4. 创建一个SqlConnection对象,用于连接到SQL Server数据库。 ```csharp using System.Data.SqlClient; SqlConnection connection = new SqlConnection("Data Source=serverName;Initial Catalog=databaseName;Integrated Security=True"); connection.Open(); ``` 5. 创建一个SqlCommand对象,用于执行插入语句。 ```csharp SqlCommand command = new SqlCommand("INSERT INTO TableName (Column1, Column2) VALUES (@Value1, @Value2)", connection); command.Parameters.AddWithValue("@Value1", cellValue1); command.Parameters.AddWithValue("@Value2", cellValue2); command.ExecuteNonQuery(); ``` 6. 在循环中使用SqlCommand对象插入数据。 7. 关闭SqlConnection对象。 ```csharp connection.Close(); ``` 完成以上步骤后,您的C# WinForm应用程序就能够将Excel数据导入到SQL Server数据库中了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值