本人想实现通过互联网数据的远程传输,具体情况如下:
某管理局下属的几个分散于各地的办公室(都具备宽带上网条件),分别安装Winform的客户端软件,在数十公里外的管理局机房的电脑装有SQL SERVER数据库,作为服务器端。为简化起见各个客户端软件界面上都只有一个Label,一个dataGridView和两个Button,Label的Text值为"12345",点击一个Button可以实现将Label的值("12345")通过宽带网传送到远端服务器并写入SQL SERVER 数据库表中,点击另一个Button可以实现将远端服务器SQL SERVER数据库表中的记录也是通过宽带网传回至客户端并在dataGridview中显示出来。怎样才能实现上述功能,最好有代码供学习,谢谢!
由于本人从未用过Web Service,想请教一下各位在Winform的程序中可不可以调用Web Service?我从网上查到一些Web Service 的应用,其中有这样一个例子:[WebMethod]
public sqlConnection getConn()
{
   string sql = "Insert into co_COD (COD_ID, COD_Line, COD_Product, COD_Qty, COD_Price, COD_Amount)";
   sql += " Values ('" + this.txtOrderId.Text + "','" + Convert.ToString(lastLine) + "','" + product + "','" + qty.Text + "','0','0'" + ")";
   SqlConnection conn  = new SqlConnection(Server=***;Database=db_News;Userid=***;password=***;");
   return conn;
}如果这样的例子能成立的话,只要把其中的 Server=***; 改成远服务器端电脑的IP,数据库名称、账号、密码改成实际可用的值,岂不是就能实现将Winform程序界面上的一些TextBox控件的当前值通过互联网写入到远程的服务器端数据库中去了吗?请教各位前辈可以实现的了吗?谢谢!

解决方案 »

  1.   

    MSDN 上看看,WebService是放在服务器上的你的应用程序只要知道Webservice的地址即可,添加一个服务引用,VS会自动生成调用代码的
      

  2.   

    可以实现,建议你把值发给web service方法,由方法连接数据库保存,比如如下伪代码:
    public bool SaveString(string txtOrderId,string lastline,string product,string qty)
    {
    string sql = "Insert into co_COD (COD_ID, COD_Line, COD_Product, COD_Qty, COD_Price, COD_Amount)";
      sql += " Values ('" + txtOrderId + "','" + lastLine + "','" + product + "','" + qty + "','0','0'" + ")";
      SqlConnection conn = new SqlConnection(Server=***;Database=db_News;Userid=***;password=***;");
      SqlCommand com = new SqlCommnad(conn);
      com.CommandText = sql;
      com.ExecuteNonQuery();
      return true;
    }
      

  3.   

    很好,谢谢楼上的。
    不过还有点疑问:
    如果以下的代码干脆连[WebMethod]也省略掉,就当是一个一般的SQL插入记录语句,放在Winform程序中用,只是数据库连接字符串用上远程电脑的IP及库表名、账号、密码,好像也应可以实现嘛,请前辈明示,谢谢。
    public bool SaveString(string txtOrderId,string lastline,string product,string qty)
    {
    string sql = "Insert into co_COD (COD_ID, COD_Line, COD_Product, COD_Qty, COD_Price, COD_Amount)";
      sql += " Values ('" + txtOrderId + "','" + lastLine + "','" + product + "','" + qty + "','0','0'" + ")";
      SqlConnection conn = new SqlConnection(Server=***;Database=db_News;Userid=***;password=***;");
      SqlCommand com = new SqlCommnad(conn);
      com.CommandText = sql;
      com.ExecuteNonQuery();
      return true;

     
      

  4.   

    当然可以实现,webservice一般是放在服务器端的,客户端只是添加web引用,引用你写好的这个webservice,然后就可以掉用里面的方法,并通过传参数在服务器端运行。
      

  5.   

    请问可不可以在Winform程序中直接用以下的代码(指不用Web Service)就能实现通过互联网向远端服务器的数据库插入新记录:
    public bool SaveString(string txtOrderId,string lastline,string product,string qty)
    {
    string sql = "Insert into co_COD (COD_ID, COD_Line, COD_Product, COD_Qty, COD_Price, COD_Amount)";
      sql += " Values ('" + txtOrderId + "','" + lastLine + "','" + product + "','" + qty + "','0','0'" + ")";
      SqlConnection conn = new SqlConnection(Server=***;Database=db_News;Userid=***;password=***;");
      SqlCommand com = new SqlCommnad(conn);
      com.CommandText = sql;
      com.ExecuteNonQuery();
      return true;
    }  
      

  6.   

    lz  这样是可以实现的  但现在有个需求 这样做的结果就是 你的这个方法会存在于所有的客户端的Winform程序中  
    假如某天 这个方法的的内部需要有些变化  你就需要更新所有客户端的Winform程序了 而采用Webservice的话 仅仅需更新服务器端的这个方法  就ok了