该解决方案使用的是"silverlight导航应用程序+Oracle数据库+WebService服务”
新建silverlight项目GH,同时会自动添加一个GH.Web,在GH.Web中添加"web 服务",同时需要添加两个XML文件用于解决跨域问题:
第一个XML文件:clientaccesspolicy.xml:
第二个XML文件:crossdomain.xml:
在GH中添加一个文件夹:Model,用于存放实体类,然后添加"服务引用":GH_ServiceReference
web服务代码:
2 [WebMethod]
3 public string getUsers()
4 {
5 try
6 {
7 OracleConnection orclConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
8 orclConn.Open();
9 OracleDataAdapter orclAdapter = new OracleDataAdapter("select U.ID,U.XM,U.SSBMMC,ZD.MS,R.JSMC from SYS_USER U,SYS_ZD ZD,SYS_ROLE R WHERE U.SSQY=ZD.ID AND U.ROLEDM=R.ID AND ZD.LXID=1 order by U.ID ", orclConn);
10 DataSet ds = new DataSet();
11 orclAdapter.Fill(ds);
12
13 int num = 1;
14 StringBuilder builder = new StringBuilder();
15 builder.Append("");
16 builder.Append("
17 foreach (DataRow row in ds.Tables[0].Rows)
18 {
19 builder.Append("
20 builder.Append("
21 builder.Append(num.ToString());
22 builder.Append("");
23 builder.Append("
24 builder.Append(row[0].ToString());
25 builder.Append("");
26 builder.Append("
27 builder.Append(row[1].ToString());
28 builder.Append("");
29 builder.Append("
30 builder.Append(row[2].ToString());
31 builder.Append("");
32 builder.Append("
33 builder.Append(row[3].ToString());
34 builder.Append("");
35 builder.Append("
36 builder.Append(row[4].ToString());
37 builder.Append("");
38 builder.Append("");
39 num++;
40 }
41 builder.Append("");
42 orclConn.Close();
43 return builder.ToString();
44 }
45 catch (Exception ex)
46 {
47 return string.Empty;
48 }
49 }
50 //删除用户
51 [WebMethod]
52 public int DelUser(int uID)
53 {
54 string sql = "delete from SYS_USER where ID="+uID.ToString();
55 return OperaData(sql);
56 }
57
58
59 [WebMethod]
60 public int InsertUser(string uName,string dep,string areaid,string roleid)
61 {
62 string sql = "insert into SYS_USER(XM,SSBMMC,SSQY,ROLEDM) values('" + uName + "','" + dep + "','" + areaid + "','" + roleid + "')";
63 return OperaData(sql);
64 }
65 [WebMethod]
66 public int UptUser(string s)
67 {
68 string [] val=s.Split('|');
69 string sql = "update SYS_USER set SSBMMC='" + val[0] + "',SSQY='" + val[1] + "',ROLEDM='" + val[2] + "' where ID='" + val[3] + "'";
70 return OperaData(sql);
71 }
72
73 ///
74 /// 增、删、改
75 ///
76 ///
77 ///
78 private int OperaData(string sql)
79 {
80 OracleConnection orclConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
81 orclConn.Open();
82 OracleCommand orclCmd = new OracleCommand(sql, orclConn);
83 int result = orclCmd.ExecuteNonQuery();
84 orclConn.Close();
85 return result;
86 }
87
88
89 ///
90 /// 获取所属区域
91 ///
92 [WebMethod]
93 public string GetArea()
94 {
95 try
96 {
97 OracleConnection orclConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
98 orclConn.Open();
99 OracleDataAdapter orclAdapter = new OracleDataAdapter("select ZD.ID,ZD.MS from SYS_ZD ZD WHERE ZD.LXID=1", orclConn);
100 DataSet ds = new DataSet();
101 orclAdapter.Fill(ds);
102
103 StringBuilder builder = new StringBuilder();
104 builder.Append("");
105 builder.Append("
106 foreach (DataRow row in ds.Tables[0].Rows)
107 {
108 builder.Append("");
109 builder.Append("
110 builder.Append(row[0].ToString());
111 builder.Append("");
112 builder.Append("
113 builder.Append(row[1].ToString());
114 builder.Append("");
115 builder.Append("");
116 }
117 builder.Append("");
118 orclConn.Close();
119 return builder.ToString();
120 }
121 catch (Exception e)
122 {
123 return string.Empty;
124 }
125 }
126
127 ///
128 /// 获取角色
129 ///
130 [WebMethod]
131 public string GetRole()
132 {
133 try
134 {
135 OracleConnection orclConn = new OracleConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
136 orclConn.Open();
137 OracleDataAdapter orclAdapter = new OracleDataAdapter("select ROLE.ID,ROLE.JSMC from SYS_ROLE ROLE", orclConn);
138 DataSet ds = new DataSet();
139 orclAdapter.Fill(ds);
140
141 StringBuilder builder = new StringBuilder();
142 builder.Append("");
143 builder.Append("
144 foreach (DataRow row in ds.Tables[0].Rows)
145 {
146 builder.Append("
147 builder.Append("
148 builder.Append(row[0].ToString());
149 builder.Append("");
150 builder.Append("
151 builder.Append(row[1].ToString());
152 builder.Append("");
153 builder.Append("");
154 }
155 builder.Append("");
156 orclConn.Close();
157 return builder.ToString();
158 }
159 catch (Exception e)
160 {
161 return string.Empty;
162 }
163 }
164 #endregion
主页面视图:
主页面xmal代码:
2 {
3 GH_ServiceReference.GH_serviceSoapClient client = new GH.GH_ServiceReference.GH_serviceSoapClient();
4 public GH_User()
5 {
6 InitializeComponent();
7 GH_ServiceReference.GH_serviceSoapClient client = new GH.GH_ServiceReference.GH_serviceSoapClient();
8 client.getUsersCompleted += new EventHandler
9 client.getUsersAsync();
10 }
11
12 ///
13 /// 获取用户信息
14 ///
15 private void OnGetUsersCompleted(object sender, GH.GH_ServiceReference.getUsersCompletedEventArgs e)
16 {
17 if (e.Error != null)
18 {
19 return;
20 }
21 XmlReader reader = XmlReader.Create(new StringReader(e.Result.ToString()));
22 XDocument doc = XDocument.Load(reader);
23 var info = from items in doc.Descendants("user")
24 select new gh_UserModel()
25 {
26 Num = (int)items.Element("number"),
27 ID = (string)items.Element("userID"),
28 Name = (string)items.Element("userName"),
29 DepName = (string)items.Element("userDep"),
30 Area = (string)items.Element("userArea"),
31 RoleID = (string)items.Element("userRole")
32 };
33 //分页
34 PagedCollectionView view = new PagedCollectionView(info.ToList());
35 dataGrid1.ItemsSource = view;
36 dataPager1.Source = view;
37 dataPager1.PageSize = 5;
38 }
39
40 //删除用户
41 private void bt_del_Click(object sender, RoutedEventArgs e)
42 {
43 client.DelUserCompleted += new EventHandler
44 client.DelUserAsync(Convert.ToInt32((sender as Button).Tag.ToString()));
45 }
46 void client_DelUserCompleted(object sender, GH_ServiceReference.DelUserCompletedEventArgs e)
47 {
48 if (e.Error == null)
49 {
50 MessageBox.Show("删除成功!");
51 //重新绑定
52 client.getUsersCompleted += new EventHandler
53 client.getUsersAsync();
54 }
55 else
56 {
57 MessageBox.Show(e.Error.ToString());
58 }
59 }
60
61 private void Page_Loaded(object sender, RoutedEventArgs e)
62 {
63 client.GetAreaCompleted += new EventHandler
64 client.GetAreaAsync();
65 client.GetRoleCompleted += new EventHandler
66 client.GetRoleAsync();
67 }
68
69 void client_GetRoleCompleted(object sender, GH_ServiceReference.GetRoleCompletedEventArgs e)
70 {
71 if (e.Error == null)
72 {
73 XmlReader reader = XmlReader.Create(new StringReader(e.Result.ToString()));
74 XDocument doc = XDocument.Load(reader);
75 var role = from items in doc.Descendants("role")
76 select new gh_RoleModel()
77 {
78 RoleID=(string)items.Element("id"),
79 RoleName = (string)items.Element("roleName")
80 };
81
82 comboBox_role.ItemsSource = role.ToList();
83 comboBox_role.DisplayMemberPath = "RoleName";
84 comboBox_role.UpdateLayout();
85 comboBox_role.SelectedIndex = 0;
86 }
87 }
88
89 void client_GetAreaCompleted(object sender, GH_ServiceReference.GetAreaCompletedEventArgs e)
90 {
91 if (e.Error == null)
92 {
93 XmlReader reader = XmlReader.Create(new StringReader(e.Result.ToString()));
94 XDocument xdoc = XDocument.Load(reader);
95 var area = from items in xdoc.Descendants("area")
96 select new gb_ZDModel()
97 {
98 ID = (string)items.Element("id"),
99 Des = (string)items.Element("ms")
100 };
101 comboBox_area.ItemsSource = area.ToList();
102 comboBox_area.DisplayMemberPath = "Des";
103 comboBox_area.UpdateLayout();
104 comboBox_area.SelectedIndex = 0;
105 }
106 }
107 //保存用户
108 private void btn_Sava_Click(object sender, RoutedEventArgs e)
109 {
110 if (string.IsNullOrEmpty(textBox1.Text))
111 {
112 MessageBox.Show("请输入用户名");
113 return;
114 }
115 if (string.IsNullOrEmpty(textBox2.Text))
116 {
117 MessageBox.Show("请输入所属部门");
118 return;
119 }
120 var areaname = comboBox_area.SelectedItem;
121 string areaid = ((gb_ZDModel)areaname).ID;
122
123 var rolename = comboBox_role.SelectedItem;
124 string roleid = ((gh_RoleModel)rolename).RoleID;
125
126 client.InsertUserCompleted += new EventHandler
127 client.InsertUserAsync(textBox1.Text, textBox2.Text, areaid, roleid);
128 }
129
130 void client_InsertUserCompleted(object sender, GH_ServiceReference.InsertUserCompletedEventArgs e)
131 {
132 if (e.Error == null)
133 {
134 client.getUsersCompleted += new EventHandler
135 client.getUsersAsync();
136 MessageBox.Show("用户添加完毕.");
137 textBox1.Text = "";
138 textBox2.Text = "";
139 comboBox_area.SelectedIndex = 0;
140 comboBox_role.SelectedIndex = 0;
141 }
142 else
143 {
144 MessageBox.Show(e.Error.ToString());
145 }
146 }
147
148 gh_User_Child user_child;
149 //修改
150 private void btn_upt_Click(object sender, RoutedEventArgs e)
151 {
152 gh_UserModel row = (gh_UserModel)dataGrid1.SelectedItem;
153
154 user_child = new gh_User_Child(row.ID, row.Name, row.DepName, row.Area, row.RoleID);
155 user_child.Title = "用户信息";
156 user_child.Show();
157 user_child.Closed += new EventHandler(user_child_Closed);
158 }
159
160 void user_child_Closed(object sender, EventArgs e)
161 {
162 if (user_child.DialogResult == true)
163 {
164 string dep = user_child.textBox2.Text;
165 string area = ((gb_ZDModel)user_child.comboBox_area.SelectedItem).ID;
166 string role = ((gh_RoleModel)user_child.comboBox_role.SelectedItem).RoleID;
167 string uID=user_child.OKButton.Tag.ToString();
168 client.UptUserCompleted += new EventHandler
169 string s = dep +"|"+ area+"|" + role +"|"+ uID;
170 client.UptUserAsync(s);
171 }
172 }
173
174 void client_UptUserCompleted(object sender, GH_ServiceReference.UptUserCompletedEventArgs e)
175 {
176 if (e.Error == null)
177 {
178 MessageBox.Show("更新成功");
179 client.getUsersCompleted += new EventHandler
180 client.getUsersAsync();
181 }
182 }
子窗体视图:
子窗体代码:
2 public gh_User_Child()
3 {
4 InitializeComponent();
5 }
6
7 public gh_User_Child(string id, string name, string dep, string area, string role)
8 {
9 InitializeComponent();
10
11 client.GetAreaCompleted += new EventHandler
12 client.GetAreaAsync();
13 client.GetRoleCompleted += new EventHandler
14 client.GetRoleAsync();
15
16 this.OKButton.Tag = id;
17 textBox1.Text = name;
18 textBox2.Text = dep;
19 comboBox_area.SelectedValue = area; //无效
20 comboBox_area.SelectedValue = role;
21 }
22
23 void client_GetRoleCompleted(object sender, GH_ServiceReference.GetRoleCompletedEventArgs e)
24 {
25 if (e.Error == null)
26 {
27 XmlReader reader = XmlReader.Create(new StringReader(e.Result.ToString()));
28 XDocument doc = XDocument.Load(reader);
29 var role = from items in doc.Descendants("role")
30 select new gh_RoleModel()
31 {
32 RoleID = (string)items.Element("id"),
33 RoleName = (string)items.Element("roleName")
34 };
35
36 comboBox_role.ItemsSource = role.ToList();
37 comboBox_role.DisplayMemberPath = "RoleName";
38 comboBox_role.UpdateLayout();
39 comboBox_role.SelectedIndex = 0;
40 }
41 }
42
43 void client_GetAreaCompleted(object sender, GH_ServiceReference.GetAreaCompletedEventArgs e)
44 {
45 if (e.Error == null)
46 {
47 XmlReader reader = XmlReader.Create(new StringReader(e.Result.ToString()));
48 XDocument xdoc = XDocument.Load(reader);
49 var area = from items in xdoc.Descendants("area")
50 select new gb_ZDModel()
51 {
52 ID = (string)items.Element("id"),
53 Des = (string)items.Element("ms")
54 };
55 comboBox_area.ItemsSource = area.ToList();
56 comboBox_area.DisplayMemberPath = "Des";
57 comboBox_area.UpdateLayout();
58 comboBox_area.SelectedIndex = 0;
59 }
60 }
61
62 private void OKButton_Click(object sender, RoutedEventArgs e)
63 {
64 this.DialogResult = true;
65 }
66 private void CancelButton_Click(object sender, RoutedEventArgs e)
67 {
68 this.DialogResult = false;
69 }