日期:2014-05-18  浏览次数:20865 次

从本地数据库改为webservice数据调用的问题

下面是我使用本地数据库时所运用的一个类文件,我现在要将它改为在webservice下调用,但因为以前没用过webservice,不知道应怎样修改,请各位指点下

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Top_Kindergarten.topdll
{  
  class dataconn
  {  
  public static string GetConnString()
  {
  return System.Configuration.ConfigurationSettings.AppSettings["Connstr"];
  }

  public void datagridload(string seldgview,System.Windows.Forms.DataGridView DGview)
  {
  SqlConnection conn = new SqlConnection(GetConnString());  
  conn.Open();

  SqlCommand cmd = new SqlCommand();
  cmd.Connection = conn;
  cmd.CommandText = seldgview; 
  SqlDataAdapter sa = new SqlDataAdapter(cmd.CommandText, conn);
  DataSet ds = new DataSet();
  sa.Fill(ds);
  DGview.DataSource = ds.Tables[0];
  conn.Close();
  }

  public void combobox_data(string str_combo, string tablename,string dpmember, string vlmember, System.Windows.Forms.ComboBox CBbox)
  {  
  SqlConnection conn = new SqlConnection(GetConnString());
  conn.Open();
  string mySql = str_combo; //"Select * from Customers";
  SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySql, conn);
  DataSet myDataSet= new DataSet();
  mySqlDataAdapter.Fill(myDataSet, tablename);

  CBbox.DataSource = myDataSet.Tables[tablename];
  CBbox.DisplayMember = dpmember;
  CBbox.ValueMember = vlmember;
  conn.Close();
  }

  public void listbox_data(string str_ltbox, string tablename, string dpmember, string vlmember, System.Windows.Forms.ListBox LTbox)
  {
  SqlConnection conn = new SqlConnection(GetConnString());
  conn.Open();
  string mySql = str_ltbox; //"Select * from Customers";
  SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySql, conn);
  DataSet myDataSet = new DataSet();
  mySqlDataAdapter.Fill(myDataSet, tablename);

  LTbox.DataSource = myDataSet.Tables[tablename];
  LTbox.DisplayMember = dpmember;
  LTbox.ValueMember = vlmember;
  conn.Close();
  }
  }
}


------解决方案--------------------
类似combobox_data方法都是完成控件的数据绑定,webservice不好做。你可以试着用webservice返回一个datatable后者dataset然后做绑定。
------解决方案--------------------
网上搜一下吧,很多
------解决方案--------------------
新建一干web services,在web.config里面配置数据库连接字符串,把写的方法添加[webmethod]属性,然后在要使用web服务的工程添加对该web 服务的引用!
------解决方案--------------------

给个例子
http://blog.csdn.net/downmoon/archive/2007/10/11/1819860.aspx