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?