我正在處理兩個數據庫。從服務器中的第一個數據庫中,我使用存儲過程將所有表和本地計算機插入到一個空數據庫中。Sql - 未連接到本地數據庫
這是我的存儲過程:
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;[email protected]";
con.Open();
SqlConnection conn = new SqlConnection(connectionstringLOCAL); //string connectionstringLOCAL = "Data Source=Clientname;Initial Catalog=Audit;uid=sa;[email protected]";
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。
現在怎麼辦?