| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 688 人关注过本帖
标题:各位DR:连接SQL2000数据库错误.请指教......
只看楼主 加入收藏
uukkcc
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2007-7-24
收藏
 问题点数:0 回复次数:6 
各位DR:连接SQL2000数据库错误.请指教......
大家好:
最近用VS 2005连SQL 2000总是不能成功.郁闷的不行了!!!
代码没错.可就是提示连接错误 (有图)



以下是代码,请各位大人指教!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;


namespace 动态绑定
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;


private SqlConnection con;
private SqlDataAdapter apt;
private DataSet ds;
public Form1()
{
InitializeComponent();
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(24, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(544, 336);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(456, 376);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "查询";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 429);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
con = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=test");
con.Open();
apt = new SqlDataAdapter("select * from test",con);
ds = new DataSet("test");
apt.Fill(ds,"test");
this.dataGrid1.DataSource = ds;
}
catch(SqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}


}
}


图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: 数据库 指教 
2007-11-15 11:20
zmfttkl
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2007-7-1
收藏
得分:0 
帮你顶一下!

2007-11-15 12:16
zklwzh
Rank: 2
等 级:论坛游民
帖 子:275
专家分:25
注 册:2005-11-16
收藏
得分:0 
你可以先拉一个sqldatasource,然后按向导操作。再把里面的那个连接字符串粘过来试试。

2007-11-15 13:04
imbug
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2007-8-17
收藏
得分:0 
先看看是否是数据库登录的方式错误!!
2007-11-18 14:05
jyoseyi
Rank: 1
等 级:新手上路
帖 子:173
专家分:0
注 册:2007-6-14
收藏
得分:0 
看下服务器是否启动,
或者试下用其他的连接,127.0.0.1
以前也遇到过这样的问题,忘记了怎么解决的,都3个月没摸键盘了,
祝你好运!

我非英雄,广目无双, 我本坏蛋,无限嚣张!
2007-11-18 15:44
卡卡艾
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:672
专家分:0
注 册:2007-4-3
收藏
得分:0 
你服务器管理器启动了吗?

革命尚未成功,同志仍需努力-----+++
2007-11-18 21:17
sean88
Rank: 1
等 级:新手上路
帖 子:146
专家分:0
注 册:2007-3-27
收藏
得分:0 


下面是解决办法:
1、在sql server外围应用配置器,进行服务和连接的外围应用配置器里面进行设置,里面有远程连接的选项
2、在sql server configuration manager里面把tcp/ip和named pipes协议的开启
3、在数据库属性中有是否允许远程连接打开
4、开启登录的帐户远程连接权限
5、还有注意要重新启动服务

连接sql server 2005时,我们需要用到的连接字串为:
"server=.\\sqlexpress;uid=帐号;pwd=密码;database=数据库名称"
而一般这个连接字串"server=localhost;user id=帐号;password=密码;database=数据库名称"



[此贴子已经被作者于2007-11-19 9:51:22编辑过]


Knowledge is infinite.
2007-11-19 09:50
快速回复:各位DR:连接SQL2000数据库错误.请指教......
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014057 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved