如何连接sqlserver数据库

如何连接sqlserver数据库,第1张

远程连接sql server 2000服务器的解决方案

一、 看ping 服务器IP能否ping通。

这个实际上是看和远程sql server 2000服务器的物理连接是否存在。如果不行,请检查网络,查看配置,当然得确保远程sql server 2000服务器的IP拼写正确。

二 、在Dos或命令行下输入telnet 服务器IP 端口,看能否连通。

如telnet 202114100100 1433

通常端口值是1433,因为1433是sql server 2000的对于Tcp/IP的默认侦听端口。如果有问题,通常这一步会出问题。通常的提示是“……无法打开连接,连接失败"。

如果这一步有问题,应该检查以下选项。

1 检查远程服务器是否启动了sql server 2000服务。如果没有,则启动。

2 检查服务器端有没启用Tcp/IP协议,因为远程连接(通过因特网)需要靠这个协议。检查方法是,在服务器上打开 开始菜单->程序->Microsoft SQL Server->服务器网络实用工具,看启用的协议里是否有tcp/ip协议,如果没有,则启用它。

3 检查服务器的tcp/ip端口是否配置为1433端口。仍然在服务器网络实用工具里查看启用协议里面的tcp/ip的属性,确保默认端口为1433,并且隐藏服务器复选框没有勾上。

事实上,如果默认端口被修改,也是可以的,但是在客户端做telnet测试时,写服务器端口号时必须与服务器配置的端口号保持一致。如果隐藏服务器复选框被勾选,则意味着客户端无法通过枚举服务器来看到这台服务器,起到了保护的作用,但不影响连接,但是Tcp/ip协议的默认端口将被隐式修改为2433,在客户端连接时必须作相应的改变。

4 如果服务器端 *** 作系统打过sp2补丁,则要对windows防火墙作一定的配置,要对它开放1433端口,通常在测试时可以直接关掉windows防火墙(其他的防火墙也关掉最好)。

5 检查服务器是否在1433端口侦听。如果服务器没有在tcp连接的1433端口侦听,则是连接不上的。检查方法是在服务器的dos或命令行下面输入

netstat -a -n 或者是netstat -an,在结果列表里看是否有类似 tcp 127001 1433 listening 的项。如果没有,则通常需要给sql server 2000打上至少sp3的补丁。其实在服务器端启动查询分析器,输入 select @@version 执行后可以看到版本号,版本号在802039以下的都需要打补丁。

如果以上都没问题,这时再做telnet 服务器ip 1433 测试,将会看到屏幕一闪之后光标在左上角不停闪动。恭喜,马上可以开始在企业管理器或查询分析器连接了。

三、 检查客户端设置

程序->Microsoft SQL Server -> 客户端网络使用工具。像在服务器网络实用工具里一样,确保客户端tcp/ip协议启用,并且默认端口为1433(或其他端口,与服务器端保持一致就行)。

四、 在企业管理器里或查询那分析器连接测试

企业管理器->右键SQlserver组->新建sqlserver注册->下一步->写入远程IP->下一步->选Sqlserver登陆->下一步->写入登陆名与密码(sa,password)->下一步->下一步->完成

查询分析器->文件->连接->写入远程IP->写入登录名和密码(sa,password)->确定

通常建议在查询分析器里做,因为默认情况下,通过企业管理器注册另外一台SQL Server的超时设置是4秒,而查询分析器是15秒。

修改默认连接超时的方法:

企业管理器->工具->选项->在d出的"SQL Server企业管理器属性"窗口中,点击"高级"选项卡->连接设置->在 登录超时(秒) 后面的框里输入一个较大的数字

查询分析器->工具->选项->连接->在 登录超时(秒) 后面的框里输入一个较大的数字

通常就可以连通了,如果提示错误,则进入下一步。

五、 错误产生的原因通常是由于SQL Server使用了"仅 Windows"的身份验证方式,因此用户无法使用SQL Server的登录帐户(如 sa )进行连接。解决方法如下所示:

1 在服务器端使用企业管理器,并且选择"使用 Windows 身份验证"连接上 SQL Server。

2 展开"SQL Server组",鼠标右键点击SQL Server服务器的名称,选择"属性",再选择"安全性"选项卡。

3 在"身份验证"下,选择"SQL Server和 Windows "。

4 重新启动SQL Server服务。(在dos或命令行下面net stop mssqlserver停止服务,net start mssqlserver启动服务,也是一种快捷的方法)。

附注:在连接本地服务器时,通常使用的是命名管道协议(在服务器网络实用工具里可以看到启用的协议有这个),默认端口是445,因此在本地能连通是不能说明什么问题的,连接远程服务器是完全不同的协议)

下面的例子就是调用通用类的数据库 *** 作方法(数据库的链接与关闭都在通用类中),不懂得花可以发例子给你。using System;

using SystemCollectionsGeneric;

using SystemText;using TroubledTimesModels;

using SystemData;

using SystemDataSqlClient;namespace TroubledTimesDAL

{

/// <summary>

/// 官方活动信息数据访问类

/// </summary>

public static class FunctionsService

{

/// <summary>

/// 1根据不同情况查询活动信息

/// </summary>

/// <param name="type">活动类型</param>

/// <param name="state">设置状态</param>

/// <param name="name">活动名称</param>

/// <param name="flag">控制变量</param>

/// <returns>活动信息对象的集合</returns>

public static IList<Functions> GetAllFunctions(string type,string state,string name,int flag)

{

string sql = "Select from Functions where State =1";

if(type!="" && flag==1)

sql += " and FunState='" + type + "'";

else if (state != "" && flag == 2)

sql += " and SetState='" + state + "'";

else if (name!="" && flag==3)

sql += " and FunctionName like '%" + name + "%'";

else if (flag == 4)

sql += " and FunState='" + type + "' and SetState='" + state + "'";

else if (flag == 5)

sql += " and FunState='" + type + "' and FunctionName like '%" + name + "%'";

else if (flag == 6)

sql += " and SetState='" + state + "' and FunctionName like '%" + name + "%'";

else if (flag == 7)

sql += " and FunState='" + type + "' and SetState='" + state + "' and FunctionName like '%" + name + "%'";

sql += " order by FunNumber Desc";

IList<Functions> list = new List<Functions>();

try

{

// DataTable dt = DBHelperGetScalar("up_SelectFunctions");

DataTable dt = DBHelperGetDataTable(sql);

foreach (DataRow row in dtRows)

{

Functions function = new Functions();

functionFunctionName = (string)row["FunctionName"];

functionFId = (int)row["FId"];

functionFunctionUrl = (string)row["FunctionUrl"];

functionFunctionImg = (string)row["FunctionImg"];

functionFunctionContent = (string)row["FunctionContent"];

functionFunctionTime = (DateTime)row["FunctionTime"];

functionFunAdminUrl = (string)row["FunAdminUrl"];

functionFunState = (int)row["FunState"]; //--活动类型(游戏活动/官网活动,0:游戏)<后加>

functionSetState = (int)row["SetState"]; //--设置状态(设置中/预设置,0:预设置)<后加>

functionFunNumber = (int)row["FunNumber"]; //--活动支持率(仅官网)<后加>

functionState = (int)row["State"]; //--存贮状态(0/1)

listAdd(function);

}

return list;

}

catch (Exception ex)

{

ConsoleWriteLine(exMessage);

return null;

}

} /// <summary>

/// 2根据活动类型获取活动信息

/// </summary>

/// <param name="id">活动类型</param>

/// <returns>该活动类型的数量</returns>

public static int GetFunctionsByType(int type)

{

IList<Functions> list = new List<Functions>();

try

{

string sql = "select count() from Functions where SetState = 1 and FunState='" + type+ "'";

return DBHelperSanlar(sql); }

catch (Exception ex)

{

ConsoleWriteLine(exMessage);

return 0;

}

} /// <summary>

/// 3根据活动ID修改活动信息

/// </summary>

/// <param name="f">活动信息类对象</param>

/// <returns>数据库中受影响的行数</returns>

public static int ModifyFunctionsById(Functions f)

{

try

{

SqlParameter[] para = new SqlParameter[]

{

new SqlParameter("@FId",fFId),

new SqlParameter("@FunctionName",fFunctionName),

new SqlParameter("@FunctionUrl",fFunctionUrl),

new SqlParameter("@FunctionImg",fFunctionImg),

new SqlParameter("@FunctionContent",fFunctionContent),

new SqlParameter("@FunctionTime",fFunctionTime),

new SqlParameter("@functionFunAdminUrl",fFunAdminUrl),

new SqlParameter("@FunState",fFunState),

new SqlParameter("@FunNumber",fFunNumber),

new SqlParameter("@SetState",fSetState),

new SqlParameter("@State",fState)

};

return DBHelperExecuteProc("up_AmendFunctions", para);

}

catch (Exception ex)

{

ConsoleWriteLine(exMessage);

return 0;

}

} /// <summary>

/// 4添加活动信息

/// </summary>

/// <param name="f">活动信息类对象</param>

/// <returns>数据库中受影响的行数</returns>

public static int AddFunctions(Functions f)

{

try

{

SqlParameter[] para = new SqlParameter[]

{

new SqlParameter("@FunctionName",fFunctionName),

new SqlParameter("@FunctionUrl",fFunctionUrl),

new SqlParameter("@FunctionImg",fFunctionImg),

new SqlParameter("@FunctionContent",fFunctionContent),

new SqlParameter("@FunctionTime",fFunctionTime),

new SqlParameter("@FunAdminUrl",fFunAdminUrl),

new SqlParameter("@FunState",fFunState),

new SqlParameter("@FunNumber",fFunNumber),

new SqlParameter("@SetState",fSetState),

new SqlParameter("@State",fState) };

return DBHelperExecuteProc("up_AddFunctions", para);

}

catch (Exception ex)

{

ConsoleWriteLine(exMessage);

return 0;

} }

/// <summary>

/// 5、根据id批量删除活动信息(修改)

/// </summary>

/// <param name="id">活动信息id</param>

/// <returns>返回受影响的行数</returns>

public static int DeleteFunById(string ids)

{

//string sql = "update Functions set State = 0 where FId in('"+ids+"')";

string sql = "update Functions set State = 0 where FId ='" + ids + "'";

try

{

return DBHelperExecuteCommand(sql);

}

catch(Exception ex)

{

throw ex;

}

}

/// <summary>

/// 6、根据id修改设置状态

/// </summary>

/// <param name="id">活动信息id</param>

/// <returns>返回受影响的行数</returns>

public static int UpdateSetById(int id, int setState)

{

string sql = "Update Functions set SetState = "+setState+" where FId="+id;

try

{

return DBHelperExecuteCommand(sql);

}

catch(Exception ex)

{

throw ex;

}

}

}

}

建立远程数据库链接,然后在你需要同步的表上建触发器:每当表有修改时,自动修改远程数据库的相应表。

----------------------------------

抱歉,你的题目我没理解正确,其实你使用双机热备就行了,在网上找到一个教程,是这个网址:

你看一下吧。

----------------------------------

不是,我先前的理解不正确,后来的理解才正确,现在是你开始走弯路了,你就按照我给的网址学习双机热备吧,肯定能达到你的目的。

另:送你关于远程数据库映射的东西:

SqlServer数据库:

--这句是映射一个远程数据库

EXEC sp_addlinkedserver '远程数据库的IP或主机名',N'SQL Server'

--这句是登录远程数据库

EXEC sp_addlinkedsrvlogin '远程数据库的IP或主机名', 'false', NULL, '登录名', '密码'

--登录后,可以用以下格式 *** 作远程数据库中的对象

select from [远程数据库的IP或主机名][数据库名][dbo][表名]

我仔细考虑了一下,用这种方式无法满足你的要求,所以请你用双机热备完成。

-------------------------------

原来是要在互联网环境下啊,你一直没有提,汗。。。

我水平有限,这个没什么好的思路,帮不上你,抱歉了。

我所能想到的,只有两种不成熟的思路:

1、用***架出局网环境,这个你可能条件所限,不能实现。

2、第二种思路是以我浅薄的知识想像出来的笨方法,拐弯抹角的而且不安全,大体思路就是,在B服务器上架构一个WEB服务器,用ASP或JSP什么的做一个页面,可以接收字串参数并作为sql语句执行(当然你要做好验证工作),然后修改A服务器的程序,使其每当对数据库有修改 *** 作时,就同时向B机的WEB主页发送一个页面申请(比如 AA set BB='cc'),这样B主机的WEB服务就会同步执行这个SQL语句了。(注意这种方法需要你的B机是以固定IP模式连网的,这需要向电信局申请)

说实话这第二种方法连我自己都觉得可笑,但水平实在有限,所以在这儿只是大胆说出我的想法,还请楼主或是其它高手看了不要见笑。

以上就是关于如何连接sqlserver数据库全部的内容,包括:如何连接sqlserver数据库、asp.net中数据库连接的公共类的调用方法、想请教一下关于aspx中数据库的 *** 作问题。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9867118.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存