WCF读取数据库中的数据传输至WPF显示
来源:互联网 发布:火凤凰云计算拖欠工资 编辑:程序博客网 时间:2024/06/05 05:27
一.WCF
1.打开visual studio (我用的vs2015),并新建一个WCF项目
2.工具->连接到数据库->更改->sql server 数据库文件
3.浏览->选择你要保存数据库文件的路径、文件名写一个你要命名的文件名(文件不存在也没事,它会自动创建)
4. 打开IService1.cs->在接口IService1中添加如下代码声明函数
[OperationContract] void CreateTable(); [OperationContract] string GetDataByTable(); [OperationContract]void AddDataToTable(string x, string y);
5. 打开Service1.svc.cs->添加using指令并在里面添加代码完成IService1.cs中声明的函数
Using指令:
using System.Configuration;using System.Data.SqlClient;using System.Data;
函数:
public void CreateTable() { string connstring1 = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\zcc9618_CSDN\blog\Web\w1\1.mdf;Integrated Security=True;Connect Timeout=30"; string commstring1 = @"CREATE TABLE StuDatabase(num CHAR(20) PRIMARY KEY,name CHAR(20));INSERT INTO StuDatabase VALUES('01','zhao');INSERT INTO StuDatabase VALUES('02','qian');INSERT INTO StuDatabase VALUES('03','sun');INSERT INTO StuDatabase VALUES('04','li');"; SqlConnection conn1 = new SqlConnection(connstring1); try { conn1.Open(); SqlCommand comm1 = new SqlCommand(commstring1, conn1); comm1.ExecuteNonQuery(); conn1.Close(); } catch (Exception) { throw; } } public string GetDataByTable() { string connstring = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\zcc9618_CSDN\blog\Web\w1\1.mdf;Integrated Security=True;Connect Timeout=30"; string message = ""; SqlConnection conn = new SqlConnection(connstring); conn.Open(); try { SqlCommand comm = new SqlCommand("SELECT * FROM StuDatabase;", conn); SqlDataReader read = comm.ExecuteReader(); while (read.Read()) { message = message + read[0].ToString() + "" + read[1].ToString() + "\r\n"; } read.Close(); } catch (Exception) { } finally { conn.Close(); } return message; } public void AddDataToTable(string x, string y) { string connstring2 = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\zcc9618_CSDN\blog\Web\w1\1.mdf;Integrated Security=True;Connect Timeout=30"; string commstring2 = @"INSERT INTO StuDatabase VALUES('" + x + "','" + y + "');"; SqlConnection conn2 = new SqlConnection(connstring2); conn2.Open(); try { SqlCommand comm2 = new SqlCommand(commstring2, conn2); comm2.ExecuteNonQuery(); } catch (Exception) { } finally { conn2.Close(); } }
6. 添加web窗体:WebForm1
7.WebForm1前台WebForm1.aspx构建
<form id="form1" runat="server"> <div> </div> <asp:Button ID="BtnStart" runat="server" Text="open server" Width="140px" OnClick="BtnStart_Click" /> <asp:Button ID="BtnClose" runat="server" Text="close server" Width="140px" OnClick="BtnClose_Click" /> <p> <asp:Label ID="Label1" runat="server" Text="Server running condition"></asp:Label> </p> </form>
8. WebForm1后台WebForm1.aspx.cs构建:添加using指令及代码
using指令:
using System.ServiceModel;
代码:
private ServiceHost host = null; protected void Page_Load(object sender, EventArgs e) { } protected void BtnStart_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(WCF_15030535_November.Service1)); host.Open(); Label1.Text = "server is open"; } protected void BtnClose_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(WCF_15030535_November.Service1)); if (host.State != CommunicationState.Closed)//判断服务是否关闭 { host.Close();//关闭服务 } this.Label1.Text = "server is closed"; }
9. Web.config修改
(1)<system.serviceModel>下添加:
<bindings> <wsHttpBinding> <binding name="userHttp"> <security mode="Message" > <message clientCredentialType="Windows"/> </security> </binding> </wsHttpBinding></bindings> <services> <!--添加服务--> <service name="WcfService1.Service1" behaviorConfiguration="CalculatorServiceBehavior"> <!--name 必须与代码中的host实例初始化的服务一样behaviorConfiguration 行为配置 --> <host> <baseAddresses> <!--添加调用服务地址--> <add baseAddress="http://localhost:8800/"/> </baseAddresses> </host> <!--添加契约接口 contract="WcfDemo.IService1" WcfDemo.IService1为契约接口 binding="wsHttpBinding" wsHttpBinding为通过Http调用--> <endpoint address="http://localhost:8800/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint> </service> </services>
(2)给原来的behavior命名
<behavior name="CalculatorServiceBehavior">
10. 项目属性中修改web中属性与Web.config中http://localhost:8800/一致
二.WPF
1. 新建一个WPF文件
2. WPF前台
<Window x:Class="WPF_15030535_November.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPF_15030535_November" mc:Ignorable="d" Title="MainWindow" Height="800" Width="1200"> <Grid Margin="0,-26,-8,0" Background="AliceBlue"> <Button Content="Create table" Height="90" HorizontalAlignment="Left" Margin="130,35,0,0" Name="button3" VerticalAlignment="Top" Width="322" Click="button3_Click" FontSize="50" Foreground="#FF4C72B4"/> <Button Content="Get data" Height="90" HorizontalAlignment="Left" Margin="836,35,0,0" Name="button1" VerticalAlignment="Top" Width="310" Click="button1_Click" FontSize="50" Foreground="#FF4C72B4" RenderTransformOrigin="0.348,0.589"/> <Button Content="Add data" Height="90" HorizontalAlignment="Left" Margin="496,35,0,0" Name="button2" VerticalAlignment="Top" Width="302" Click="button2_Click" FontSize="50" Foreground="#FF4C72B4"/> <TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Visible" HorizontalAlignment="Left" Margin="122,294,0,133" Name="textBox1" Width="1033" TextChanged="textBox1_TextChanged" /> <TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="43" Margin="122,188,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="500" FontSize="24"/> <Label Content="add" HorizontalAlignment="Left" Margin="36,188,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.35,0.423" Height="43" Width="62" FontSize="24" Foreground="#FF4C72B4"/> <TextBox x:Name="textBox3" HorizontalAlignment="Left" Height="41" Margin="655,190,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="500" FontSize="24"/> <Label Content="get" HorizontalAlignment="Left" Margin="42,294,0,0" VerticalAlignment="Top" FontSize="24" Width="56" Foreground="#FF4C72B4"/> </Grid></Window>
3. wpf后台:添加引用并添加using指令
using指令:
using WcfService1;
代码:
public MainWindow() { InitializeComponent(); } private void button2_Click(object sender, RoutedEventArgs e) { Service1 ser1 = new Service1(); ser1.AddDataToTable(textBox2.Text, textBox3.Text); } private void button1_Click(object sender, RoutedEventArgs e) { textBox1.Text = ""; Service1 ser2 = new Service1(); textBox1.Text = textBox1.Text + "num\t\tname\r\n"; textBox1.Text = textBox1.Text + ser2.GetDataByTable().ToString(); } private void textBox1_TextChanged(object sender, TextChangedEventArgs e) { } private void button3_Click(object sender, RoutedEventArgs e) { Service1 ser3 = new Service1(); ser3.CreateTable(); }
阅读全文
0 0
- WCF读取数据库中的数据传输至WPF显示
- .Net读取数据库中的图片并显示
- WPF WCF
- WCF - 大数据传输
- WCF大数据传输
- WCF全双工数据传输
- flex wcf 数据传输
- WCF大数据传输
- WCF 大数据传输问题
- WCF大数据传输
- 读取并显示已写入ORACLE数据库中的图片
- 将读取数据库中的数据显示在表格之中
- JavaScript读取xml文档中的数据库,并分页显示
- WPF读取和显示word的方法
- [WCF 学习笔记] 8. 数据传输
- 读取数据库中的图片
- WPF连接数据库+显示数据到dataGrid
- WPF连接数据库+显示数据到dataGrid
- JSP内建对象介绍
- C语言基础学习(2)
- 金融初学者建议
- 表格全选与全不选,选中删除
- Pandas日期数据处理:如何按日期筛选、显示及统计数据
- WCF读取数据库中的数据传输至WPF显示
- ipython notebook 下包含python2, python3两个kernel
- Linux(C/C++)下的文件操作open、fopen与freopen
- Misha and Changing Handles
- Wannafly挑战赛1-C.MMset2(LCA)
- [LeetCode] 34. Search for a Range
- Window使用问题积累
- java知识点概括
- HTML