日期:2014-05-20 浏览次数:20808 次
public interface ICalculator//定义接口 { [OperationContract] bool InsertOrUpdateOrDeleteData(string cmdText); [OperationContract] ObservableCollection<Student> SelectDate(string cmdText); } public class CalculatorService : ICalculator //实现接口 { public bool InsertOrUpdateOrDeleteData(string cmdText) { return DBhelper.GetExcuteNonQuery(cmdText); } public ObservableCollection<Student> SelectDate(string cmdText) { DataSet ds = new DataSet(); ds=DBhelper.GetData(cmdText); ObservableCollection<Student> obj = new ObservableCollection<Student>(); DataTable dt = new DataTable(); dt = ds.Tables[0]; for (int i = 0; i < dt.Rows.Count; i++) { obj.Add(new Student() { Stu_id = (string)dt.Rows[i][0], Stu_Name = (string)dt.Rows[i][1], Stu_Age = (int)dt.Rows[i][2], Stu_Adress = (string)dt.Rows[i][3], Stu_Sex = (bool)dt.Rows[i][4], Stu_Info = (string)dt.Rows[i][5] }); } return obj; } } //实现对定义在Services项目中的服务的寄宿 using (ServiceHost Host = new ServiceHost(typeof(CalculatorService))) { //终结点 Host.AddServiceEndpoint(typeof(ICalculator), new BasicHttpBinding(), "http://127.0.0.1:8080/CalculatorService"); if (Host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) { ServiceMetadataBehavior behavior = new ServiceMetadataBehavior(); behavior.HttpGetEnabled = true; behavior.HttpGetUrl = new Uri("http://127.0.0.1:8080/CalculatorService/metadata"); Host.Description.Behaviors.Add(behavior); } Host.Opened += delegate { Console.WriteLine("输入off关闭服务"); string str = Console.ReadLine(); if (str == "off") { Host.Close(); } }; Host.Open(); Console.ReadLine(); } public class DBhelper : IDisposable { private static string connctionString = "User=sa;Password=123456;Database=test; Server=."; private static SqlConnection conn; private static SqlCommand cmd; private static SqlDataAdapter dr; /// <summary> /// 得到连接对象 /// </summary> public static SqlConnection Connection { get { conn = new SqlConnection(connctionString); conn.Open(); return conn; } } /// <summary> /// 关闭连接时,释放资源 /// </summary> public void Dispose() { conn.Dispose(); cmd.Dispose(); } /// <summary> /// 关闭操作数据库的对象 /// </summary> public void Closed() { cmd.Cancel(); if (conn.State == ConnectionState.Closed) { conn.Close(); } } /// <summary> /// 创建参数 /// </summary> /// <param name="ParaName"></param> /// <param name="type"></param> /// <param name="value"></param> /// <returns></returns> public static SqlParameter CreateParameter(string ParaName, SqlDbType type, object value) { SqlParameter Para = new SqlParameter(ParaName, type); Para.Value=value ; return Para ; } /// <summary> /// 增删改 /// </summary> /// <param name="cmdText"></param> /// <returns></returns> public static bool GetExcuteNonQuery(string cmdText) { using (cmd = new SqlCommand(cmdText,Connection)) { if (cmd.ExecuteNonQuery() > 0) { return true; }else { return false ; } } } /// <summary> /// 查询返回数据 /// </summary> /// <param name="cmdText"></param> /// <returns></returns> public static DataSet GetData(string cmdText) { DataSet ds = new DataSet(); using (dr = new SqlDataAdapter(cmdText,Connection)) { dr.Fill(ds); return ds; } } //在SL类库项目中调用 CalculatorServiceClient calient = new CalculatorServiceClient("http://127.0.0.1:8080/CalculatorService"); calient.SelectDateCompleted += new EventHandler<SelectDateCompletedEventArgs>(calient_SelectDateCompleted); CalculatorServiceClient("http://127.0.0.1:8080/CalculatorService"); //实例化时报错,关键字不在字典中 //在控制台程序中调用 class Program { static void Main(string[] args) { CalculatorServiceClient cl = new CalculatorServiceClient(); Student[] obj= cl.SelectDate("select * from students"); foreach (var item in obj) { Console.WriteLine(((Student)item).Stu_Name); } Console.ReadLine(); } } //可以正常输出 //网上的资料都说 要创建SL wcf工程...大牛们..在SL中怎么调用这个WCF服务呢 //引用WCF生成的CONFIG,我看是对的啊... <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_CalculatorService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://127.0.0.1:8080/CalculatorService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_CalculatorService" contract="ServiceReference1.CalculatorService" name="BasicHttpBinding_CalculatorService" /> </client> </system.serviceModel> </configuration>