gpt4 book ai didi

c# - 创建 Windows 服务时 SchedularCallBack 出错

转载 作者:行者123 更新时间:2023-11-30 23:01:42 25 4
gpt4 key购买 nike

我什么都试过了。查看了之前所有类似的问题(其中将近 14k),但无法找到解决方案。

我无法从我的服务连接到我的数据库。这是错误

Simple Service Error on: 16/06/2018 11:50:00 PM Login failed for user 'LAPTOP-84OBORRF\sriva'.

跟踪继续并结束于

at WindowsServiceCS.Service1.SchedularCallback(Object e) in D:\Projects\WindowsService_SendEmail\WindowsServiceCS\Service1.cs:line 113

这是我的连接字符串

<connectionStrings>
<add name="constr" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=VMS.Models.ApplicationDbContext; User Id=LAPTOP-84OBORRF\sriva; Password = 1234; "/>
</connectionStrings>

最后是我的调度函数

try
{
DataTable dt = new DataTable();
string query = "SELECT Name, EmailAdress FROM VolunteerInfoes WHERE DATEPART(DAY, BirthdayDateTime) = @Day AND DATEPART(MONTH, BirthdayDateTime) = @Month";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Day", DateTime.Today.Day);
cmd.Parameters.AddWithValue("@Month", DateTime.Today.Month);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt); //The error is in this line
}
}
}
foreach(DataRow row in dt.Rows)
{
string name = row["Name"].ToString();
string email = row["EmailAdress"].ToString();
WriteToFile("Trying to send email to: " + name + " " + email);

using (MailMessage mm = new MailMessage("srivastava.anan@gmail.com", email))
{
mm.Subject = "Birthday Greetings";
mm.Body = string.Format("<b>Happy Birthday </b>{0}<br /><br />Many happy returns of the day.", name);

mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential();
credentials.UserName = "srivastava.anan@gmail.com";
credentials.Password = "footube@-321";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credentials;
smtp.Port = 587;
smtp.Send(mm);
WriteToFile("Email sent successfully to: " + name + " " + email);
}
}
this.ScheduleService();
}
catch (Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);

//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}

在我创建的服务中,有人能告诉我帐户密码和连接字符串中的密码是否需要相同吗?那是, enter image description here

这个密码和连接字符串中的密码?谢谢。

编辑

DB Image

当我点击我的数据库的属性时显示的连接字符串

Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=VMS.Models.ApplicationDbContext;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

我从以前的答案中尝试的解决方案,

已添加

 <system.web>
<authentication mode="Windows"/>
</system.web>

或将 Integrated security 更改为 False 以指定用户 ID 和密码。

尝试了许多其他解决方案,但无法正常工作。请帮我。谢谢。

最佳答案

所以我在连接字符串中指定了 Windows 帐户和密码。这是不允许的;这些仅用于使用 SQL 帐户进行身份验证。所以我在连接字符串中添加了“Integrated Security=SSPI”并删除了用户 ID 和密码,我的问题就解决了。

关于c# - 创建 Windows 服务时 SchedularCallBack 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50890962/

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