2011-04-08 51 views
1

我有本地IIS服务器,本地SQL服务器和WCF测试服务,3接口中的方法:WCF IIS SQL问题

[OperationContract] 
string GetData(int value); 

[OperationContract] 
CompositeType GetDataUsingDataContract(CompositeType composite); 

[OperationContract] 
string getFirstName(); 

前两个是VS模板,我已经添加了getFirstName()方法

{ 
    //very basic 
    string connectionString = @"Data Source=.\SqlExpress;Initial Catalog=ProjectDB;Integrated Security=True"; 

    SqlConnection con = new SqlConnection(connectionString); 
    con.Open(); 

    SqlCommand command = new SqlCommand("select * from messages;", con); 

    DataTable table = new DataTable(); 

    SqlDataAdapter adapter = new SqlDataAdapter(command); 
    adapter.Fill(table); 

    con.close(); 

    return table.Rows[0][3].ToString(); 
} 

当我在VS的WCF测试客户端上测试调用时,所有方法都按照它们应该运行。

当使用VS向导(发布 - >本地IIS - > MyTestWeb站点)将服务发布到本地IIS时,尝试调用getFirstName()(SQL的身份验证方法是Windows身份验证)时出现错误。

其他方法:

CompositeType GetDataUsingDataContract(CompositeType composite); 

string GetData(int value); 

工作在两个VS开发服务器和本地IIS服务器上的完美。

谢谢。

+2

**你得到什么**错误?请告诉我们 - 我们无法看到并阅读您的屏幕! – 2011-04-08 15:07:45

+1

你的意思是你没有使用新的.NET 4。5 System.ESP.dll程序集?它仍然在CTP,但它有一个上线许可证。 – RQDQ 2011-04-08 15:32:31

+0

marc_s我实际上看不到error \ exception(因为我不知道如何在IIS上运行服务),但我试图提供可以工作的东西。很高兴知道如何在IIS上捕获错误(仍然那是一个新手)。谢谢 。 – Stas 2011-04-08 18:17:12

回答

0

最有可能的,你的出现错误,因为你使用的是集成安全性连接到SQL Server Express,并在IIS,这意味着其运行IIS的身份,以及该帐户将最有可能你的SQL进行许可服务器实例。

另一个建议:你应该改进你的ADO.NET代码!

  • 使用using(.....) { .... }块保护您的一次性实体(尤其在WCF环境的重要!)

  • 不创建SqlDataAdapterDataTable只是读取单个列值... ...

我的建议是这样的:

public string GetFirstName() 
{ 
    string firstName = string.Empty; 

    string connectionString = @"Data Source=.\SqlExpress;Initial Catalog=ProjectDB;Integrated Security=True"; 

    using(SqlConnection con = new SqlConnection(connectionString)) 
    using(SqlCommand command = new SqlCommand("SELECT FirstName FROM dbo.Messages", con)) 
    { 
     con.Open(); 

     using(SqlDataReader rdr = command.ExecuteReader()) 
     { 
      if(rdr.Read()) 
      { 
      firstName = rdr.GetString(0); // read column no. 0 
      } 

      rdr.Close(); 
     } 

     con.close(); 
    } 

    return firstName; 
} 

这并不能解决您当前的问题,但它可能会帮助您避免将来(也很难找到/跟踪)这些问题!

+0

我的歉意,完全忘记标记答案。 – Stas 2011-04-23 23:43:18

+0

..谢谢你的回答,这真是一个userAuthority问题。 – Stas 2011-04-23 23:44:08

0

是的,这可能是权限。确保用户的站点运行对SQL Server数据库具有权限 - 例如NT Authority \ SYSTEM(这取决于您的IIS版本)。

1

进一步从后端检索单个值时,最合适的使用方法可能是“ExecuteScalar”,它是专门为该任务设计的,正如名称所示。