0

I'm working with two databases. From 1st database in the server I'm taking all the tables and inserting in local machine in one empty database using a stored procedure.

This is my stored procedure:

SET ANSI_NULLS ON
 GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[ImportScriptAndData]

@Branch int,
@MainServer varchar(20),
@LocalServer varchar(20),
@LocalDBName varchar(20),
@AuditType int

AS Begin

set nocount on;


-- Delete the master tables

-- dbo.AuditArea
print ('delete from ['+ @LocalServer +'].['+ @LocalDBName +'].[dbo].AuditArea')
exec  ('delete from ['+ @LocalServer +'].['+ @LocalDBName +'].[dbo].AuditArea')

-- Insert data into master tables

-- dbo.AuditArea
print ('insert into ['+ @LocalServer +'].[HDFCAudit].dbo.AuditArea
select * from ['+ @MainServer +'].[HDFCAudit].dbo.AuditArea')
exec ('insert into ['+ @LocalServer +'].['+ @LocalDBName +'].dbo.AuditArea
select * from ['+ @MainServer +'].['+ @LocalDBName +'].dbo.AuditArea')

End

This stored procedure is on server database.

I'm connecting two computers on lan. One is server and other is client

On client application I have a button that can be clicked when I want to bring all the tables from server database to local empty database.

My click button code is:

SqlConnection con = new SqlConnection(newConn);  //string newConn = "Data Source=Servername;Initial Catalog=Audit;uid=sa;pwd=admin@123";
            con.Open();
            SqlConnection conn = new SqlConnection(connectionstringLOCAL); //string connectionstringLOCAL = "Data Source=Clientname;Initial Catalog=Audit;uid=sa;pwd=admin@123";
            conn.Open();
            BranchCode = Convert.ToInt32(cboBranch.SelectedValue.ToString());
            MainServer = "Servername";
            LocalServer = "ClientName"; //express
            LocalDatabase = "Audit";
            AuditType = 1;
            SqlCommand cmd = new SqlCommand("dbo.ImportScriptAndData", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@Branch", SqlDbType.Int)).Value = BranchCode;
            cmd.Parameters.Add(new SqlParameter("@MainServer", SqlDbType.VarChar)).Value = MainServer;
            cmd.Parameters.Add(new SqlParameter("@LocalServer", SqlDbType.VarChar)).Value = LocalServer;
            cmd.Parameters.Add(new SqlParameter("@LocalDBName", SqlDbType.VarChar)).Value = LocalDatabase;
            cmd.Parameters.Add(new SqlParameter("@AuditType", SqlDbType.Int)).Value = AuditType;
            //cmd.Parameters.AddWithValue("@Branch", BranchCode);
            //cmd.Parameters.AddWithValue("@MainServer", MainServer);
            //cmd.Parameters.AddWithValue("@LocalServer", LocalServer);
            //cmd.Parameters.AddWithValue("@LocalDBName", LocalDatabase);
            cmd.ExecuteNonQuery(); // **error on this line**

And I'm getting following error on this line:

Could not find server 'clientname' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

What to do now?

4

2 に答える 2

0

2台のサーバー間で分散クエリを実行します。見られるのは2つのサーバーをリンクする必要があります。

USE master;
GO
EXEC sp_addlinkedserver 
   N'Other Server',
   N'SQL Server';
GO

リンク: http: //msdn.microsoft.com/fr-fr/library/ms190479.aspx

于 2012-09-26T09:08:56.400 に答える
0

まずは表をチェック

select * from sys.servers 

サーバー名「clientname」が含まれているかどうかを確認します。

そうでない場合は、挿入操作を実行するためにリンク サーバーを追加する必要があります。これは、サーバー マシンでクライアント サーバー名をリンク サーバーとして追加する必要があることを意味します。

または

C#.net SqlBulkCopyメソッドを使用して、データ転送を簡単に実行できます。

于 2012-09-26T08:20:58.443 に答える