Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
Sql server 2008 SQL Server 2008-使用Scripter.EnumScript的脚本数据失败,返回“0”;用户“登录失败”;错误_Sql Server 2008_Smo - Fatal编程技术网

Sql server 2008 SQL Server 2008-使用Scripter.EnumScript的脚本数据失败,返回“0”;用户“登录失败”;错误

Sql server 2008 SQL Server 2008-使用Scripter.EnumScript的脚本数据失败,返回“0”;用户“登录失败”;错误,sql-server-2008,smo,Sql Server 2008,Smo,我正在尝试为表的数据编写脚本 如果我使用windows身份验证(integratedsecurity=true),代码就会正常工作 如果我使用现有用户和密码切换到sql身份验证,它将无法尝试访问返回的可枚举脚本。用户是SQL登录名和管理员 代码的简化版本如下: //string connectionString = "Data Source=myserver; Initial Catalog=mydb; Integrated Security=True";

我正在尝试为表的数据编写脚本

如果我使用windows身份验证(integratedsecurity=true),代码就会正常工作

如果我使用现有用户和密码切换到sql身份验证,它将无法尝试访问返回的可枚举脚本。用户是SQL登录名和管理员

代码的简化版本如下:

    //string connectionString = "Data Source=myserver; Initial Catalog=mydb; Integrated Security=True";            
    string connectionString = "Data Source=myserver; Initial Catalog=mydb; Integrated Security=False; User ID=user1; Password=1234;";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        ServerConnection serverConnection = new ServerConnection(connection);
        Server server = new Server(serverConnection);

        Database database = server.Databases["mydb"];
        Collection<SqlSmoObject> tables = new Collection<SqlSmoObject>();
        tables.Add(database.Tables["mytable"]);

        Scripter scripter = new Scripter(server);
        scripter.Options.ScriptData = true;
        scripter.Options.ScriptSchema = false;
        var scripts = scripter.EnumScript(tables.ToArray());

        Collection<string> coll = new Collection<string>();
        foreach (var item in scripts) // error is here
        {
            coll.Add(item);
        }
    }
更不经常的情况是:

System.Data.SqlClient.SqlException occurred
  Message=A connection was successfully established with the server, but then an error occurred during the login process. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  Class=20
  LineNumber=0
  Number=233
  Server=myserver
  State=0
  StackTrace:
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
  InnerException: 

知道为什么会这样吗?

看起来像是一个脚本编写错误

Scripter可能使用原始连接的ConnectionString属性关闭现有连接并重新打开另一个连接。当然,如果安全信息没有持久化,则该连接字符串没有密码信息

因此,解决方法是使用安全信息创建连接:

string connectionString = "Data Source=myserver; Initial Catalog=mydb; Integrated Security=False; User ID=user1; Password=1234; Persist Security Info=True;";
// etc.
string connectionString = "Data Source=myserver; Initial Catalog=mydb; Integrated Security=False; User ID=user1; Password=1234; Persist Security Info=True;";
// etc.