日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
WCF技巧:教你如何輕松訪問數(shù)據(jù)庫 (wcf 如何訪問數(shù)據(jù)庫)

WCF(Windows Communication Foundation)是一種用于創(chuàng)建分布式應(yīng)用程序的框架,它能夠幫助開發(fā)人員在不同的平臺上進行通信。在開發(fā)中,我們常常需要訪問數(shù)據(jù)庫來存儲和讀取數(shù)據(jù),而WCF也提供了一系列的服務(wù)和類,支持對數(shù)據(jù)庫進行數(shù)據(jù)訪問。本篇文章將教你如何使用WCF輕松訪問數(shù)據(jù)庫,讓你的開發(fā)更加高效。

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的白山網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

一、設(shè)置數(shù)據(jù)庫連接

在WCF中,我們需要先設(shè)置數(shù)據(jù)庫連接,讓應(yīng)用程序知道訪問哪個數(shù)據(jù)庫,并通過哪個賬戶進行訪問。在配置文件Web.config或App.config中添加以下代碼:

“`xml

“`

在這段代碼中,我們定義了數(shù)據(jù)庫連接的名稱myConnectionString,以及連接字符串和提供程序名稱。需要注意的是,Data Source表示數(shù)據(jù)庫服務(wù)器的地址,Initial Catalog表示要訪問的數(shù)據(jù)庫名稱,User Id表示登錄賬戶的用戶名,Password表示登錄賬戶的密碼,providerName表示提供程序的名稱。

二、創(chuàng)建數(shù)據(jù)訪問類

在WCF中,我們可以通過數(shù)據(jù)訪問類訪問數(shù)據(jù)庫,以讀取或修改數(shù)據(jù)庫中的數(shù)據(jù)。WCF中提供了許多數(shù)據(jù)訪問類,比如ADO.NET、Entity Framework等,本篇文章以ADO.NET為例來演示如何如何使用數(shù)據(jù)訪問類訪問數(shù)據(jù)庫。

我們需要創(chuàng)建一個數(shù)據(jù)訪問類,該類可以實現(xiàn)接口IUserRepository,并實現(xiàn)以下方法:

“`csharp

public class UserRepository : IUserRepository

{

private string connectionString = ConfigurationManager.ConnectionStrings[“myConnectionString”].ConnectionString;

public IEnumerable GetAllUsers()

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

connection.Open();

SqlCommand command = new SqlCommand(“SELECT * FROM Users”, connection);

SqlDataReader reader = command.ExecuteReader();

List users = new List();

while (reader.Read())

{

User user = new User();

user.UserId = Convert.ToInt32(reader[“UserId”]);

user.UserName = reader[“UserName”].ToString();

user.PhoneNumber = reader[“PhoneNumber”].ToString();

users.Add(user);

}

reader.Close();

return users;

}

}

public User GetUserById(int userId)

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

connection.Open();

SqlCommand command = new SqlCommand(“SELECT * FROM Users WHERE UserId=@UserId”, connection);

command.Parameters.AddWithValue(“@UserId”, userId);

SqlDataReader reader = command.ExecuteReader();

if (reader.Read())

{

User user = new User();

user.UserId = Convert.ToInt32(reader[“UserId”]);

user.UserName = reader[“UserName”].ToString();

user.PhoneNumber = reader[“PhoneNumber”].ToString();

reader.Close();

return user;

}

else

{

reader.Close();

return null;

}

}

}

}

“`

在上面的代碼中,我們創(chuàng)建了一個UserRepository類,實現(xiàn)了接口IUserRepository,同時定義了數(shù)據(jù)庫連接字符串,和兩個方法GetAllUsers和GetUserById。其中,GetAllUsers方法從數(shù)據(jù)庫中獲取所有用戶信息,并返回一個列表;GetUserById方法從數(shù)據(jù)庫獲取指定用戶的信息,并返回該用戶的User對象。

三、創(chuàng)建服務(wù)契約

在WCF中,我們需要創(chuàng)建服務(wù)契約,該契約定義了服務(wù)提供的方法、數(shù)據(jù)類型和返回值。在本例中,我們需要創(chuàng)建一個服務(wù)契約,它可以提供讀取用戶信息的方法。代碼如下:

“`csharp

[ServiceContract]

public interface IUserService

{

[OperationContract]

IEnumerable GetAllUsers();

[OperationContract]

User GetUserById(int userId);

}

“`

在上面的代碼中,我們創(chuàng)建了一個IUserService服務(wù)契約,定義了兩個服務(wù)方法GetAllUsers和GetUserById,分別對應(yīng)上面的UserRepository類中的兩個方法。

四、實現(xiàn)服務(wù)

創(chuàng)建完服務(wù)契約后,我們需要創(chuàng)建服務(wù)并實現(xiàn)服務(wù)契約中的方法。代碼如下:

“`csharp

public class UserService : IUserService

{

private IUserRepository userRepository = new UserRepository();

public IEnumerable GetAllUsers()

{

return userRepository.GetAllUsers();

}

public User GetUserById(int userId)

{

return userRepository.GetUserById(userId);

}

}

“`

在上面的代碼中,我們創(chuàng)建了UserService類,并實現(xiàn)了IUserService中的兩個方法GetAllUsers和GetUserById,這兩個方法調(diào)用了UserRepository類中的方法完成數(shù)據(jù)讀取的操作。

五、配置服務(wù)

完成服務(wù)的實現(xiàn)后,我們需要進行配置,將服務(wù)發(fā)布到IIS中,以便其他應(yīng)用程序可以調(diào)用。打開Web.config文件,添加以下配置項:

“`xml

“`

在上面的配置中,我們將服務(wù)名稱設(shè)置為UserService,并且使用basicHttpBinding協(xié)議來與客戶端進行通信。同時,設(shè)置了服務(wù)的行為,其中serviceMetadata httpGetEnabled=”true”表示服務(wù)可以通過http方式獲取元數(shù)據(jù)信息,serviceDebug includeExceptionDetlInFaults=”false”表示出現(xiàn)異常時,不將詳細信息返回給客戶端。

我們需要將服務(wù)發(fā)布到IIS中,以便其他應(yīng)用程序可以調(diào)用。在Visual Studio中選擇Build> Publish,設(shè)置發(fā)布文件夾和發(fā)布方式,點擊Publish按鈕完成發(fā)布。

六、調(diào)用服務(wù)

運行服務(wù)后,在瀏覽器中輸入服務(wù)地址,可以看到服務(wù)的元數(shù)據(jù)信息。接下來,我們可以使用客戶端應(yīng)用程序來調(diào)用服務(wù)。在客戶端中,需要添加服務(wù)引用,并創(chuàng)建客戶端代理對象來調(diào)用服務(wù)。

“`csharp

static void Mn(string[] args)

{

UserServiceClient userServiceClient = new UserServiceClient();

var userList = userServiceClient.GetAllUsers();

foreach (var user in userList)

{

Console.WriteLine(user.UserId + “,” + user.UserName + “,” + user.PhoneNumber);

}

Console.ReadLine();

}

“`

在上面的代碼中,我們首先創(chuàng)建了一個UserServiceClient客戶端代理對象,通過該對象來訪問服務(wù)。然后,我們調(diào)用了服務(wù)中的GetAllUsers方法,獲取所有用戶信息,并遍歷輸出。調(diào)用Console.ReadLine方法來保持控制臺窗口打開。

七、

相關(guān)問題拓展閱讀:

  • 如何直接訪問網(wǎng)站的數(shù)據(jù)庫
  • silverlight調(diào)用WCF讀取數(shù)據(jù)庫,會寫填充到datagrid,但是我想用linq查詢某一條符合條件的數(shù)據(jù)到文本框中,

如何直接訪問網(wǎng)站的數(shù)據(jù)庫

1、先創(chuàng)建程序所要訪問的數(shù)據(jù)庫,打開控制梁渣面板,建立ODBC數(shù)據(jù)源:開始→設(shè)置→控制面板→管理工具→ODBC數(shù)據(jù)源→系統(tǒng)DSN。

2、然后編寫數(shù)據(jù)庫訪問程序:在程序中首先要加載驅(qū)動,其次要建立連接,再次創(chuàng)建嫌段用于訪問數(shù)據(jù)庫的芹渣譽Statement對象,然后利用Statement對象訪問數(shù)據(jù)庫

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

Stringurl=”jdbc:odbc:book”;

Stringquery=”SELECT*FROMbook1″;

Connectionc=DriverManager.getConnection(Url,user,password);

Connectioncon=DriverManager.getConnection(url);

Statementstmt=con.createStatement();

ResultSetrs=stmt1.executeQuery(query);

建立連接之后,可以訪問數(shù)據(jù)庫,對數(shù)據(jù)庫進行操作,如:查詢、修改、刪除。

連接使用完畢,可以調(diào)用close()方法關(guān)閉連接.

importjava.sql.*;

classSimpleSelect{

publicstaticvoidmain(Stringargs){

Stringurl=”jdbc:odbc:book”;

Stringquery=”SELECT*FROMbook1″;

try{

Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);

}

catch(java.lang.ClassNotFoundExceptione){

System.err.print(“ERROR:”);

System.err.print(e.getMessage());

}

//輸出數(shù)據(jù)庫中信息

try{

Connectioncon=DriverManager.getConnection(url);

Statementstmt=con.createStatement();

ResultSetr=stmt.executeQuery(query);

System.out.println(“id:”+””+”name”+””+”price”+””+”author”+””+”publish_name”);

while(r.next()){

Longr1=r.getLong(“id”);

Stringr2=r.getString(“name”);

Doubler3=r.getDouble(“price”);

Stringr4=r.getString(“author”);

Stringr5=r.getString(“publish_name”);

System.out.println(r1+””+r2+””+r3+””+r4+””+r5);

}

r.close();

stmt.close();

con.close();

}catch(SQLExceptionex){

ex.printStackTrace();

}

}

silverlight調(diào)用WCF讀取數(shù)據(jù)庫,會寫填充到datagrid,但是我想用linq查詢某一條符合條件的數(shù)據(jù)到文本框中,

var query =

from order in MemoryData.dsCreditSports.Tables.AsEnumerable()

join gaminginfo in MemoryData.dsCreditSports.Tables.AsEnumerable()

on order.Field(“playid”) equals

gaminginfo.Field(“playid”) into temp1

from play in temp1.DefaultIfEmpty()

join tradingitem in MemoryData.dsCreditSports.Tables.AsEnumerable()

on order.Field(“tradingitemid”) equals

tradingitem.Field(“tradingitemid”) into temp2

from trade in temp2.DefaultIfEmpty()

//where order.Field(“OnlineOrderFlag”) == true

//&& order.Field(“OrderDate”).Month == 8

select new

{//intradingAmt▲totAmt▲processStatus▲playId▲tradingDirection▲processDesc▲matchId▲matchAmt▲tradingItemId▲loginName issuedate

intradingamt =

order.Field(“intradingamt”),

matchAmt =

order.Field(“matchAmt”),

//playid =

// order.Field游辯(“playid”),

tradingitemname =

trade == null ? “” : trade.Field(“tradingitemname”),

playname =

play == null ? “” : play.Field(“playname”),

tradingdirectionname =

order.Field(“tradingdirection”) == “” ? “” :

order.Field(“tradingdirection”) == CommonConst.TradingDirectionEnum.buy.ToString(“d”) ? “買神腔缺” : “賣”,

};

foreach (var item in query)

{

DataRow dRows = MemoryData.dsCreditSports.Tables.Rows;

dRows = string.IsNullOrEmpty(item.playname) ? “不限圓和” : item.playname;

dRows = string.IsNullOrEmpty(item.tradingitemname) ? “不限” : item.tradingitemname;

dRows = string.IsNullOrEmpty(item.tradingdirectionname) ? “不限” : item.tradingdirectionname;

dRows = string.IsNullOrEmpty(item.intradingamt) ? “0” : item.intradingamt;

dRows = string.IsNullOrEmpty(item.matchAmt) ? “0” : item.matchAmt;

}

這是我在項目中用到的,給你參考下吧

不是很清楚你遇到的問題是不知道怎么用wcf還是態(tài)明補助到怎么用Linq.

如果是不知道怎么用wcf,這個一時半會是說不清楚的,建議你看看教程;

如果是不清楚怎么用linq 這個就簡單了:

你的接口寫成:

//假設(shè)你的EntityFramework實例名為:

ExampleEntities edm = new ExampleEntities();

public UserInfo GetUserInfo(int id)

{

//可以用linq to sql,不過不清楚你的數(shù)據(jù)表結(jié)構(gòu),這里就不介紹了

//因為linq經(jīng)常配合ORM共同使用,這里以其中一種EntityFramework為例:

var query = edm.UserInfo.FirstOrDefault(s => s.ID == id);

return (UserInfo)query;

}

wcf 如何訪問數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于wcf 如何訪問數(shù)據(jù)庫,WCF技巧:教你如何輕松訪問數(shù)據(jù)庫,如何直接訪問網(wǎng)站的數(shù)據(jù)庫,silverlight調(diào)用WCF讀取數(shù)據(jù)庫,會寫填充到datagrid,但是我想用linq查詢某一條符合條件的數(shù)據(jù)到文本框中,的信息別忘了在本站進行查找喔。

成都網(wǎng)站推廣找創(chuàng)新互聯(lián),老牌網(wǎng)站營銷公司
成都網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)(www.cdcxhl.com)專注高端網(wǎng)站建設(shè),網(wǎng)頁設(shè)計制作,網(wǎng)站維護,網(wǎng)絡(luò)營銷,SEO優(yōu)化推廣,快速提升企業(yè)網(wǎng)站排名等一站式服務(wù)。IDC基礎(chǔ)服務(wù):云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗、服務(wù)器租用、服務(wù)器托管提供四川、成都、綿陽、雅安、重慶、貴州、昆明、鄭州、湖北十堰機房互聯(lián)網(wǎng)數(shù)據(jù)中心業(yè)務(wù)。


當(dāng)前標(biāo)題:WCF技巧:教你如何輕松訪問數(shù)據(jù)庫 (wcf 如何訪問數(shù)據(jù)庫)
文章分享:http://m.5511xx.com/article/dpdjpop.html