Database connectivity in WCF Service
I am creating a WCF Service. It receives data from a iPhone App. When the user sends data from their iPhone I need to save the data into our Database useing WCF Service. In my WCF application I am creating a class and wrote the following code.
string connstr = ConfigurationManager.ConnectionStrings["myconn"].ToString();
string SQLstr开发者_运维技巧 = "myStoredProc1";
SqlConnection conn = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand(SQLstr, conn);
Intellisense is giving error for (constr, SQLstr)
A field initializer cannot reference the non-static field, method or property.
Why I am getting this error. I see a lot of examples online with the same code. Then why its giving error for my application. Is it because this class is in a WCF application.
This is how it sould look like:
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);
}
}
}
精彩评论