我正在使用两个数据库。从服务器中的第一个数据库中,我使用存储过程将所有表插入到一个空数据库中的本地计算机中。
这是我的存储过程:
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
此存储过程位于服务器数据库上。
我在局域网上连接两台电脑。一个是服务器,另一个是客户端
在客户端应用程序上,当我想将所有表从服务器数据库转移到本地空数据库时,可以单击该按钮。
我的点击按钮代码是:
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**
我在这一行收到以下错误:
在 sys.servers 中找不到服务器“客户端名称”。验证是否指定了正确的服务器名称。如有必要,执行存储过程 sp_addlinkedserver 将服务器添加到 sys.servers。
现在做什么?