您的位置:首页 > 数据库

WCF读取数据库中的数据传输至WPF显示

2017-11-12 21:15 232 查看
一.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();
}

b5c2
}


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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: