gpt4 book ai didi

c# - WCF 和 SQL 错误

转载 作者:行者123 更新时间:2023-11-30 13:49:26 25 4
gpt4 key购买 nike

我正在构建一个简单的 WCF 服务,该服务必须从 SQL 表返回一些数据。当我运行该项目时,出现以下错误:

Failed to invoke the service. Possible causes: The service is offline or inaccessible; the client-side configuration does not match the proxy; the existing proxy is invalid. Refer to the stack trace for more detail. You can try to recover by starting a new proxy, restoring to default configuration, or refreshing the service

如果我注释所有的 SQL 部分并发送一些静态数据,一切都运行良好。这是让我头疼的功能:

public Client getClient(int idClient)
{
Client c = new Client();
SqlConnection sql = new SqlConnection(@"Data Source=GRIGORE\SQLEXPRESS;Initial Catalog=testWCF;Integrated Security=True");
sql.Open();

SqlCommand cmd = new SqlCommand("Select * from Clienti where id = " + idClient);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
c.idClient = int.Parse(dr["id"].ToString());
c.numeClient = dr["nume"].ToString();
}

dr.Close();
sql.Close();

return c;
}

想法?

最佳答案

您没有设置 SqlCommand 实例的 Connection 属性。你需要这样做:

    SqlCommand cmd = new SqlCommand("Select * from Clienti where id = " + idClient);
cmd.Connection = sql; // added Connection property initialization
SqlDataReader dr = cmd.ExecuteReader();

或者你可以将它注入(inject)到你的构造函数中:

    SqlCommand cmd = new SqlCommand("...your query text", sql);

关于c# - WCF 和 SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com