2011-07-21 112 views
0

我正在创建一个WCF服务。 它从iPhone应用程序接收数据。 当用户从他们的iPhone发送数据时,我需要将数据 保存到我们的数据库使用WCF服务。 在我的WCF应用程序中,我创建了一个类并编写了下面的代码。WCF服务中的数据库连接

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

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

智能感知是给错误(constr, SQLstr)

甲字段初始不能引用非静态字段,方法或 属性。

为什么我收到此错误。我使用相同的代码在线看到很多示例。 那么为什么它给我的应用程序的错误。是因为这个类在WCF应用程序中。

+0

它无关,与WCF。这是一个_compiler_错误,编译器不知道有关WCF的任何信息。它意味着它说什么! –

+0

你有这个代码的方法似乎是静态的。将其更改为Instance方法,它应该可以工作。如果这不能解决问题,请发布完整的代码。 – Asdfg

+0

它不是静态方法。它不是一种方法。这里是整个代码。使用系统; using System.Collections.Generic;使用System.Linq的 ; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; 命名空间WCFRESTService100 { 公共MyClass类 { 串conn1就= ConfigurationManager.ConnectionStrings [ “的myconn”]的ToString(); 字符串SQLstr =“myStoredProc1”; SqlConnection conn = new SqlConnection(SDconn); \t \t SqlCommand cmd = new SqlCommand(SQLstr,conn); } } – Henry

回答

1

这是怎么回事前人的精力样子:

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); 


     } 
    } 
} 
+1

-1没有'使用'块 –

+0

它的工作就像一个魅力。感谢您的信息,并非常感谢您的时间和帮助。 – Henry

+0

我刚刚给他提供了错误信息的解决方案。 – Asdfg