日期:2014-05-17 浏览次数:20551 次
//创建链接服务器
string linkserver = "EXEC master.dbo.sp_addlinkedserver @server = N'Link',@srvproduct=N'ms',@provider=N'SQLOLEDB',@datasrc=N'*.*.*.*:*'";
string linklogin = "EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'Link',@locallogin = NULL,@useself = N'false',@rmtuser=N'*admin',@rmtpassword='*";
//这个没有用到,需要怎么操作?
//判断链接服务器是否存在
string linkexist = "select count(*) from master..sysservers where srvname='Link'";
//链接字符串
string connstring = "Server=Link;Database=Book;User ID=caijiadmin;PassWord=caiji@159.com";//远程数据库1
string connstring1 = @"Data Source=.\SQLEXPRESS;AttachDbFilename=d:\sun\Desktop\Admin2\App_Data\Database.mdf;Integrated Security=True;User Instance=True";//本地数据库1
if(IsLink(connstring1,linkexist))//判断链接服务器是否存在。存在:进行数据读取;不存在:创建
{
int datanums = GetCounts(connstring, "select count(*) from Link.Book.dbo.BookInfo");
count.Text = datanums.ToString();
}
else
{
if (LinkData(connstring1, linkserver))//创建链接服务器.创建成功:读数据;失败:
{
int datanums = GetCounts(connstring, "select count(*) from Link.Book.dbo.BookInfo");
count.Text = datanums.ToString();
}
else
{ }
}
}
//返回表的记录总数
public int GetCounts(string connstring, string cmdstring)
{
using (SqlConnection conn = new SqlConnection(connstring))
{
SqlCommand cmd = new SqlCommand(cmdstring, conn);
try
{
conn.Open();
&nb