这是来公司后做的第三个实例项目,功能是Silverlight+WCF+sql Server实现对数据库数据的增删改查。过程中也遇到一些问题。不过还好解决了!刚开始以为自己做不出来呢,没想到竟然成功了!值此十一国庆佳节之际特此记录!祝大家国庆快乐!
解决方案如下:
创建过程:1.打开VS,创建silverlight程序,选择web站点承载silverlight程序;
2.在web项目中添加wcf服务,添加契约并实现相应功能;
WCF契约:
public interface IUserService { [OperationContract] List<User> RetrIEveUser(); [OperationContract] bool createuser(int userID,string username); [OperationContract] bool UpdateUser(int userID,string username); [OperationContract] bool DeleteUser(int userID); } [DataContract] public class User { [DataMember] public int UserID {get; set;} [DataMember] public string Username { get; set; } }WCF契约实现:
//查询用户 public List<User> RetrIEveUser() { List<User> user = new List<User>(); try { sqlConnection _sqlConnection =new sqlConnection("Database=test1;Server=服务器名;Integrated Security=false;password=123;user ID=sa;"); _sqlConnection.open(); sqlDataAdapter da = new sqlDataAdapter("SELECT * FROM [User] ",_sqlConnection); DataSet ds = new DataSet(); da.Fill(ds); foreach (DaTarow row in ds.tables[0].Rows) { User a = new User(); a.UserID =(int) row["UserID"]; a.Username =(string) row["Username"]; user.Add(a); } return user; } catch (Exception ex) { user.Clear(); return user; } } //创建用户 public bool createuser(int userID,string username) { try { sqlConnection _sqlConnection = new sqlConnection("Database=test1;Server=服务器名;Integrated Security=false;password=123;user ID=sa;"); _sqlConnection.open(); sqlCommand command = new sqlCommand(); command.Connection = _sqlConnection; command.CommandType = CommandType.Text; command.CommandText = "INSERT INTO [User] ([UserID],[Username]) VALUES ('" + userID+ "','" + username+ "')"; command.ExecuteNonquery(); _sqlConnection.Close(); return true; } catch (Exception ex) { return false; } } //更新用户 public bool UpdateUser(int userID,string username) { try { sqlConnection _sqlConnection = new sqlConnection("Database=test1;Server=服务器名;Integrated Security=false;password=123;user ID=sa;"); _sqlConnection.open(); sqlCommand command = new sqlCommand(); command.Connection = _sqlConnection; command.CommandType = CommandType.Text; command.CommandText = "UPDATE [User] SET [Username] = " +username + "WHERE [UserID] = " + userID; command.ExecuteNonquery(); _sqlConnection.Close(); return true; } catch (Exception ex) { return false; } } //删除用户 public bool DeleteUser(int userID) { try { sqlConnection _sqlConnection = new sqlConnection("Database=test1;Server=服务器名;Integrated Security=false;password=123;user ID=sa;"); _sqlConnection.open(); sqlCommand command = new sqlCommand(); command.Connection = _sqlConnection; command.CommandType = CommandType.Text; command.CommandText = "DELETE [User] WHERE [UserID] =" + userID; command.ExecuteNonquery(); _sqlConnection.Close(); return true; } catch (Exception ex) { return false; } }
3.在silverlight中添加wcf的服务引用。
MainPage前台:
<GrID x:name="LayoutRoot" Background="White"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center"> <StackPanel OrIEntation="Horizontal"> <TextBlock TextAlignment="Center" VerticalAlignment="Center">UserID:</TextBlock> <TextBox x:name="userID" WIDth="76" margin="21,0"></TextBox> </StackPanel> </StackPanel> <StackPanel margin="0,10,0" HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel OrIEntation="Horizontal"> <TextBlock VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Center">Username:</TextBlock> <TextBox x:name="username" WIDth="76"></TextBox> </StackPanel> </StackPanel> <StackPanel OrIEntation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel margin="0,0"> <button x:name="btnselect" Click="btnselect_Click" margin="30,0">Selectbutton</button> </StackPanel> <StackPanel margin="0,0"> <button x:name="btnupdate" Click="btnupdate_Click" margin="10,0">Updatebutton</button> </StackPanel> <StackPanel margin="0,0"> <button Click="deletebutton_Click" margin="10,0">Deletebutton</button> </StackPanel> <StackPanel margin="0,0"> <button x:name="btncreate" Click="btncreate_Click" margin="10,0">Createbutton</button> </StackPanel> </StackPanel> <StackPanel> </StackPanel> <StackPanel Height="165" margin="0,0"> <sdk:DataGrID autoGenerateColumns="False" Height="152" name="dataGrID1" WIDth="144" DataContext="{Binding}" SelectionChanged="dataGrID1_SelectionChanged"> <sdk:DataGrID.Columns> <sdk:DataGrIDTextColumn Binding="{Binding UserID,Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" header="UserID" WIDth="auto" /> <sdk:DataGrIDTextColumn Binding="{Binding Username,Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" header="Username" WIDth="auto" /> </sdk:DataGrID.Columns> </sdk:DataGrID> </StackPanel> </StackPanel> </GrID>
MainPage后台:
using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;using LYY.UserService;namespace LYY{ public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } voID deletebutton_Click(object sender,RoutedEventArgs e) { UserServiceClIEnt userSvcclIEnt = new UserServiceClIEnt(); int userID; userID = Convert.ToInt32(this.userID.Text); userSvcclIEnt.DeleteUserCompleted += new EventHandler<DeleteUserCompletedEventArgs> (userSvcclIEnt_DeleteUserCompleted); userSvcclIEnt.DeleteUserAsync(userID); } voID userSvcclIEnt_DeleteUserCompleted(object sender,DeleteUserCompletedEventArgs e) { if (e.Error == null) { MessageBox.Show("删除用户成功!"); } else { MessageBox.Show("删除用户失败!"); } } private voID btncreate_Click(object sender,RoutedEventArgs e) { UserServiceClIEnt clIEnt = new UserServiceClIEnt(); int userID; userID = Convert.ToInt32(this.userID.Text); string username = this.username.Text; clIEnt.createuserCompleted += new EventHandler<createuserCompletedEventArgs>(clIEnt_create); clIEnt.createuserAsync(userID,username); } voID clIEnt_create(object sender,createuserCompletedEventArgs e) { if (e.Result) { MessageBox.Show("用户创建成功!"); } else { MessageBox.Show("用户创建成功!"); } } private voID btnupdate_Click(object sender,RoutedEventArgs e) { UserServiceClIEnt clIEnt = new UserServiceClIEnt(); int userID; userID = Convert.ToInt32(this.userID.Text); string username = this.username.Text; clIEnt.UpdateUserCompleted += new EventHandler<UpdateUserCompletedEventArgs>(clIEnt_update); clIEnt.UpdateUserAsync(userID,username); } voID clIEnt_update(object sender,UpdateUserCompletedEventArgs e) { if (e.Result) { MessageBox.Show("用户更新成功!"); } else { MessageBox.Show("用户更新成功!"); } } private voID btnselect_Click(object sender,RoutedEventArgs e) { UserServiceClIEnt clIEnt = new UserServiceClIEnt(); clIEnt.RetrIEveUserCompleted += new EventHandler<RetrIEveUserCompletedEventArgs>(clIEnt_select); clIEnt.RetrIEveUserAsync(); } voID clIEnt_select(object sender,RetrIEveUserCompletedEventArgs e) { if (e.Error==null) { this.dataGrID1.ItemsSource = e.Result; } } private voID dataGrID1_SelectionChanged(object sender,SelectionChangedEventArgs e) { User current = new User(); current = (User)this.dataGrID1.SelectedItem; this.userID.Text = Convert.ToString(current.UserID); this.username.Text = current.Username; } }}总结
以上是内存溢出为你收集整理的Silverlight+WCF+Sql Server全部内容,希望文章能够帮你解决Silverlight+WCF+Sql Server所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)