protected void btnSave_Click(object sender, EventArgs e) { SqlParameter[] lsparam = { new SqlParameter("@server",YingSheName_500011.Text.Trim()), new SqlParameter("@srvproduct",""), new SqlParameter("@provider","SQLNCLI"), new SqlParameter("@datasrc",ShuJuYuan_500011.Text.Trim()) }; ExecuteCommand("sp_addlinkedserver",lsparam);
SqlParameter[] lsLoginParam = { new SqlParameter("@rmtsrvname",YingSheName_500011.Text.Trim()), new SqlParameter("@useself","true"), new SqlParameter("@locallogin",null), new SqlParameter("@rmtuser",txtLoginID.Text.Trim()), new SqlParameter("@rmtpassword",txtPWD.Text.Trim()) }; ExecuteCommand("sp_addlinkedsrvlogin", lsLoginParam);
} public static int ExecuteCommand(string procName,params SqlParameter[] values) { SqlCommand cmd = new SqlCommand(); cmd.Connection = Connection; cmd.CommandText = procName; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddRange(values); int result = cmd.ExecuteNonQuery(); Connection.Close(); return result; }
sp_addlinkedsrvlogin [ @rmtsrvname = ] 'rmtsrvname' [ , [ @useself = ] 'TRUE' | 'FALSE' | NULL ] [ , [ @locallogin = ] 'locallogin' ] [ , [ @rmtuser = ] 'rmtuser' ] [ , [ @rmtpassword = ] 'rmtpassword' ] A value of TRUE specifies that logins use their own credentials to connect to rmtsrvname, with the rmtuser and rmtpassword arguments being ignored. FALSE specifies that the rmtuser and rmtpassword arguments are used to connect to rmtsrvname for the specified locallogin. If rmtuser and rmtpassword are also set to NULL, no login or password is used to connect to the linked server.
protected void btnSave_Click(object sender, EventArgs e)
{
SqlParameter[] lsparam =
{
new SqlParameter("@server",YingSheName_500011.Text.Trim()),
new SqlParameter("@srvproduct",""),
new SqlParameter("@provider","SQLNCLI"),
new SqlParameter("@datasrc",ShuJuYuan_500011.Text.Trim())
};
ExecuteCommand("sp_addlinkedserver",lsparam);
SqlParameter[] lsLoginParam =
{
new SqlParameter("@rmtsrvname",YingSheName_500011.Text.Trim()),
new SqlParameter("@useself","true"),
new SqlParameter("@locallogin",null),
new SqlParameter("@rmtuser",txtLoginID.Text.Trim()),
new SqlParameter("@rmtpassword",txtPWD.Text.Trim())
}; ExecuteCommand("sp_addlinkedsrvlogin", lsLoginParam);
} public static int ExecuteCommand(string procName,params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandText = procName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
int result = cmd.ExecuteNonQuery();
Connection.Close();
return result;
}
我总感觉是你创建链接服务器的时候密码没输对,你在创建试试。
--创建链接服务器
exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '
exec sp_addlinkedsrvlogin 'ITSV ', 'false ',null, '用户名 ', '密码 ' --查询示例
select * from ITSV.数据库名.dbo.表名
exec sp_addlinkedsrvlogin 'ITSV ', 'false',null, '用户名 ', '密码 ' 这个地方我用的是true 。
有什么区别呢?求解释?
[ , [ @useself = ] 'TRUE' | 'FALSE' | NULL ]
[ , [ @locallogin = ] 'locallogin' ]
[ , [ @rmtuser = ] 'rmtuser' ]
[ , [ @rmtpassword = ] 'rmtpassword' ]
A value of TRUE specifies that logins use their own credentials to connect to rmtsrvname, with the rmtuser and rmtpassword arguments being ignored. FALSE specifies that the rmtuser and rmtpassword arguments are used to connect to rmtsrvname for the specified locallogin. If rmtuser and rmtpassword are also set to NULL, no login or password is used to connect to the linked server.