Reputation: 47
My question is the following. I'm developing a Windows Service which need to collects data from a computer and inserts into an SQL Server on an another machine (both computer are in same domain).
I install the service with the InstallUtil.exe without any problem and after when I try to start the service from the "Services" window I get the following message in the EventLog:
I understand the error message but I can't understand why I get this message because I configured the SQL Server login to mixed mode.
I developed a test Windows Form application, which can easily connect to the SQL Server from the computer where the service is also.
This is the code of my test Windows Form application which connects:
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(@"Data Source= SomeMachineNam\SQLEXPRESS;Initial Catalog=SomeDatabase;User ID=SomeUser;Password=SomePWD;");
con.Open();
if (con.State == ConnectionState.Open)
{
MessageBox.Show("OK");
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And this is the snippet of my not working service:
OnStart event:
This is an SQL Server checker calling.
protected override void OnStart(string[] args)
{
while (SQL_ellenorzo_Class.Inditaskor_ellenoriz() == false) { }
}
SQL_ellenorzo_Class
This is the SQL Server checker class
static class SQL_ellenorzo_Class
{
#region Globális változók
//A "Main" osztály példányosítása
static weight_checker_data_collector fo_osztaly = new weight_checker_data_collector();
//Az SQL kapcsolat példányosítása.
static SqlConnection con = new SqlConnection(@"Data Source="
+ _3DES_dekodolo_Class.Decrypt(ConfigurationManager.AppSettings["data_source"])
+ ";Initial Catalog="
+ _3DES_dekodolo_Class.Decrypt(ConfigurationManager.AppSettings["initial_catalog"])
+ ";User ID="
+ _3DES_dekodolo_Class.Decrypt(ConfigurationManager.AppSettings["user_id"])
+ ";Password="
+ _3DES_dekodolo_Class.Decrypt(ConfigurationManager.AppSettings["password"])
+ ";");
//Az indításkori kapcsolódási próbák száma.
internal static int kapcsolodasi_proba_indulaskor = 0;
//Mivel a leállás hosszab ideig tart, így 2x küldi el a végleges hiba üzenetet, így ezt kezelni kell, hogy el lett-e már küldve.
static bool vegleges_hiba_elkuldve = false;
#endregion
internal static bool Inditaskor_ellenoriz()
{
//Azámláló növelése, amely az indítási próbákat számolja.
kapcsolodasi_proba_indulaskor++;
//A szervíz nevének meghatározása és beállítása a 'Servicecontroller'-nek.
int processId = System.Diagnostics.Process.GetCurrentProcess().Id;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Service WHERE ProcessId = " + processId);
ManagementObjectCollection collection = searcher.Get();
fo_osztaly.Servicecontroller.ServiceName = (string)collection.Cast<ManagementBaseObject>().First()["Name"];
//A gép nevének megadása a Servicecontroller-nek.
fo_osztaly.Servicecontroller.MachineName = Environment.MachineName;
try
{
//A kapcsolat megnyitása.
con.Open();
if (con.State == System.Data.ConnectionState.Open)
{
//A kapcsolat lezárása.
con.Close();
}
//A visszatérési érték 'true'.
return true;
}
catch
{
//Ha 30-szor egymás után nem érhető el az SQL szerver, akkor e-mailt küld.
if (kapcsolodasi_proba_indulaskor > 30)
{
//Ha még nem lett elküldve a végleges hiba.
if (vegleges_hiba_elkuldve == false)
{
//Hibaüzenet az EventLog-ba, hogy nem tudott kapcsolódni az SQL szerverhez és most leáll.
fo_osztaly.Eventlog.WriteEntry("At startup the service cannot connect to the SQL server and now will stop. Please check it!", EventLogEntryType.Error);
//El lett küldve a végleges hiba.
vegleges_hiba_elkuldve = true;
//E-mail arról, hogy az elinduláskor nem tudott kapcsolódni az SQL szerverhez.
E_mail_kuldo_Class.Email_kuldes("SQL Server connecting error", "At the startup the Anritsu Data Collector service could not connect to the SQL Server. Please check it!");
}
}
//Ha még csak kevesebb, mint 30-szor vagy pont annyiszor próbált meg kapcsolódni az SQL szerverhez, akkor gyűjti az adatokat egy változóba.
else if (kapcsolodasi_proba_indulaskor <= 30)
{
//Írja az EventLog-ba, hogy hány sikertelen csatlakozási próba volt az SQL szerverhez.
fo_osztaly.Eventlog.WriteEntry("The " + kapcsolodasi_proba_indulaskor.ToString() + "/30 trying to connect to the SQL server is failed. ", EventLogEntryType.Error);
}
//A folyamat várjon 1 másodpercet.
Thread.Sleep(2000);
//Rekurzívan meghívja saját magát a metódus.
Inditaskor_ellenoriz();
//Visszatérés 'Hamis' értékkel.
return false;
}
}
I hope you can understand my problem. If you have any question please ask me.
Upvotes: 0
Views: 358
Reputation: 347
Did you enable remote accessing and creating new users to connect to your sql instance?
Upvotes: 1
Reputation: 35477
The SQL server is not configured for SQL authentication it only accepts Windows Authentication, which as a service you must specify a user name and password at installation time.
You need to change the connection string to remove user name and password or allow SQL authentication on server.
Upvotes: 0