Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Data.SqlClient.SqlException(0x80131904):用户登录失败';胡森';_C#_Sql Server_Windows Services_Database Connection - Fatal编程技术网

C# System.Data.SqlClient.SqlException(0x80131904):用户登录失败';胡森';

C# System.Data.SqlClient.SqlException(0x80131904):用户登录失败';胡森';,c#,sql-server,windows-services,database-connection,C#,Sql Server,Windows Services,Database Connection,我有一个windows服务,它应该将数据从一个数据库插入到另一个数据库 对于app.config文件中的每个db,我都有这两个连接字符串 <connectionStrings> <add name="Connection1" connectionString="User ID=husen;Password=test;Persist Security Info=False;Initial Catalog=IntalioSt

我有一个windows服务,它应该将数据从一个数据库插入到另一个数据库

对于app.config文件中的每个db,我都有这两个连接字符串

<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>
尝试启动该服务时,用户登录失败

我尝试了我在网上能找到的一切:

  • 已启用SQL Server身份验证
  • 启用命名管道和TCP/IP
  • 将用户映射到我的两个数据库
  • 作为public和sysadmin的服务器角色
  • 在onstart方法中而不是在app.config文件中写入我的连接字符串
  • 已启用并启动SQL Server浏览器服务
还有什么我应该试试的吗

以下是完整的服务代码:

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();
        }
    }
}

-您不应该将SQL语句连接在一起-而是使用参数化查询来避免SQL注入-签出是
husen
SQL登录吗?或者它是Windows帐户?请从SQL Server日志中添加完整的错误消息(您可以在左侧的SSMS中获得此消息)@AlwaysLearning它是SQL帐户login@Charlieface我正在检查事件查看器上的错误消息,sql server日志没有显示任何错误消息
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();
        }
    }
}