System.Data.SqlClient.SqlException (0x80131904): Login failed for user 'husen'

-1

I have a windows service that's supposed to insert data from one db to another

I have these two connection strings for each db in my app.config file

<connectionStrings>
    <add name="Connection1" 
         connectionString="User ID=husen;Password=test;Persist Security Info=False;Initial Catalog=IntalioStock;Data Source=(localdb)\MSSQLLocalDB;"/>
    <add name="Connection2" 
         connectionString="User ID=husen;Password=test;Persist Security Info=False;Initial Catalog=CustomersVIP;Data Source=(localdb)\MSSQLLocalDB;"/>
  </connectionStrings>

And that's what I'm using in the onstart method in the service.cs file

 con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString);
 con1.Open();

con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString);
 con2.Open();

When attempting to start the service I'm getting login failed for user.

I tried everything I could find on the web:

  • enabled SQL Server authentication
  • enabled Named Pipes and TCP/IP
  • mapped user to my two dbs
  • server roles as public and sysadmin
  • wrote my connection strings in onstart method instead of in app.config file
  • enabled and started SQL Server browser service

Any additional thing I should try?

Here's the full service code:

namespace CustomerService
{
    public partial class Service1 : ServiceBase
    {
        Timer timer1 = new Timer(); // name space(using System.Timers;)  

        public Service1()
        {
            InitializeComponent();
        }

        SqlConnection con1, con2;

        protected override void OnStart(string[] args)
        {
            // creating and opening connection to both the database
            con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString);
            con1.Open();

            con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection2"].ConnectionString);
            con2.Open();

            // enabling, setting interval and starting timer.
            // (Note: don’t use the timer in ToolBox. Add another Timer control in ToolBox
            // having namespace “System.timer.Timer” and use that timer control)
            timer1.Enabled = true;
            timer1.Interval = 10000;
            timer1.Start();
        }

        protected override void OnStop()
        {
            // stopping timer and closing connection to both the databases.
            timer1.Stop();
            con1.Close();
            con2.Close();
        }

        // code for timer elapsed event which is fired on the interval set for the
        // timer. Here it is 10 secs.
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            SqlCommand cmd;

            // creating DataAdapter for connection one(db1)

            SqlDataAdapter da = new SqlDataAdapter("Select * from Customer where [IsVIP]='True'", con1);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);

            // creating and populating table with records in database
            DataTable dt = new DataTable();
            da.Fill(dt);
            int i;

            // loop for each row in table.
            cmd = new SqlCommand("Insert into Customer values('' + dr[0].ToString() + '','' + dr[1].ToString() + '','' + dr[2].ToString() + '','' + dr[3].ToString() + '','' + dr[4].ToString() + '','' + dr[5].ToString() + '')", con2);

            foreach (DataRow dr in dt.Rows)
            {
                // inserting values in db2
                i = cmd.ExecuteNonQuery();

                // setting the value of “Check” column to true as it is updated in the other
                // database
                dr[6] = true;
            }

            // updating the data adapter, so that the changes made to the record of first
            // database(db1), reflects in original table of db1.
            da.Update(dt);

            cmd.Dispose();
            dt.Dispose();
            da.Dispose();
        }
    }
}
c#
sql-server
windows-services
database-connection
asked on Stack Overflow May 21, 2021 by h.daoud • edited May 21, 2021 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0