簡體   English   中英

WCF 服務中的數據庫連接

[英]Database connectivity in WCF Service

我正在創建一個 WCF 服務。 它從 iPhone 應用程序接收數據。 當用戶從他們的 iPhone 發送數據時,我需要使用 WCF 服務將數據保存到我們的數據庫中。 在我的 WCF 應用程序中,我正在創建一個 class 並編寫了以下代碼。

string connstr = ConfigurationManager.ConnectionStrings["myconn"].ToString();
string SQLstr = "myStoredProc1";

SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(SQLstr, conn);

Intellisense 為(constr, SQLstr)提供錯誤

字段初始值設定項不能引用非靜態字段、方法或屬性。

為什么我收到此錯誤。 我在網上看到很多使用相同代碼的示例。 那為什么它給我的應用程序錯誤。 是不是因為這個 class 在 WCF 應用程序中。

這就是它的樣子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace WCFRESTService100 
{ 
    public class myclass 
    { 
        string conn1 = ConfigurationManager.ConnectionStrings["myconn"].ToString(); 
        string SQLstr = "myStoredProc1"; 

        public void SomeMethod()
        {
            SqlConnection conn = new SqlConnection(SDconn);
            SqlCommand cmd = new SqlCommand(SQLstr, conn); 


        }
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM