我还是.NET的新手,我无法理解为什么从应用程序到数据库工作正常的sql语句现在在我尝试卸载时无效它的服务。我有一个Winform,它有一个日期,运营商和上传者复选框,根据检查的内容,sql语句应该像应用程序与数据库对话时一样遵守它。我是否必须为每个可能的选项编写OperationContract? (即,仅限日期,日期和承运人,日期和上传者等等。)谢谢!
public DataSet GetStatTableYear(StatDetails statInfo)
{
SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"SELECT CONVERT(VARCHAR(4), CreateDate, 112) AS cPeriod, COUNT(DISTINCT IMEINo) AS TotalIMEINo, COUNT(*) AS TotalImageCnt, SUM(FileSize) AS TotalFileSize
FROM VR_Image
WHERE (@Uploader IS NULL OR Uploader = @Uploader)
AND (@CarrierID IS NULL OR CarrierID = @CarrierID)
AND DeleteDate IS NULL
GROUP BY CONVERT(VARCHAR(4), CreateDate, 112)
ORDER BY cPeriod", con);
cmd.Parameters.AddWithValue("@CarrierID", statInfo.CarrierID);
cmd.Parameters.AddWithValue("@Uploader", statInfo.Uploader);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
return ds;
}
答案 0 :(得分:3)
在您使用集成安全性的连接字符串中,这意味着使用Windows身份验证来建立连接。
您的Windows窗体应用程序在Windows帐户安全上下文中运行,并且可以连接。
除非您在IIS和web.config中启用它,否则WCF应用程序很可能不使用Windows身份验证,在这种情况下,我认为IIS APP池身份/帐户无法访问数据库以建立该连接。
这是我在这个阶段最好的猜测,因为你没有解释你收到的失败细节。
答案 1 :(得分:0)
我找到了一个完美的解决方案。由于服务一直忽略了sql语句......
WHERE (Uploader IS NULL OR Uploader = @Uploader)
AND (CarrierID IS NULL OR CarrierID = @CarrierID)
当没有任何东西可用时,我将DBNull分配给它,现在它正常工作。
新代码
public DataSet GetStatTableYear(StatDetails statInfo)
{
using (SqlConnection con = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=vurdevice;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand(@"SELECT CONVERT(VARCHAR(4), CreateDate, 112) AS cPeriod, COUNT(DISTINCT IMEINo) AS TotalIMEINo, COUNT(*) AS TotalImageCnt, SUM(FileSize) AS TotalFileSize
FROM VR_Image
WHERE (Uploader IS NULL OR Uploader = @Uploader)
AND (CarrierID IS NULL OR CarrierID = @CarrierID)
AND DeleteDate IS NULL
GROUP BY CONVERT(VARCHAR(4), CreateDate, 112)
ORDER BY cPeriod", con);
cmd.Parameters.AddWithValue("@CarrierID", statInfo.CarrierID == null ? DBNull.Value.ToString() : statInfo.CarrierID);
cmd.Parameters.AddWithValue("@Uploader", statInfo.Uploader == null ? DBNull.Value.ToString() : statInfo.Uploader);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
con.Close();
return ds;
}
}