数据库连接不上(急啊100分)!!!
我用的操作系统是winxp专业版,数据库是SQLsever2000personal版
连接数据库提示:
未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。
其他信息: 系统错误。
指针指向:
SqlDataReader usdr=FPara.SqlReader("select top 1 * from DepartMent where UserID='"+UserCode+"'",FPara.connStr);
我用try{}catch{}捕捉异常出现提示对话框:
SQLServer不存在或访问被拒绝
可能是什么问题,是否与操作系统或数据库版本有关,还是其它问题!!
=========
连接数据库的模块:
using System;
using System.Data;
using System.Data.SqlClient;
namespace UserData
{
public class FPara
{
public static string ShopCode="";
public static bool CheckLogin()
{
if(ShopCode=="")
{
return false;
}
else
{
return true;
}
}
public static SqlConnection connStr=new SqlConnection("Server=localhost;uid=sa;pwd=123456;database=goods");
public static SqlDataReader SqlReader(string sql,SqlConnection connstr)
{
SqlDataReader sqldr=null;
SqlCommand cmd=new SqlCommand(sql,connstr);
if (cmd.Connection.State.ToString()=="Closed") cmd.Connection.Open();
try
{
sqldr=cmd.ExecuteReader();
}
catch(Exception e)
{
if (e!=null) sqldr=null;
}
return sqldr;
}
//数据库操作连接
public static string SqlCmd(string sql,SqlConnection connstr)
{
string errorstr=null;
SqlCommand sqlcmd= new SqlCommand(sql,connstr);
if (sqlcmd.Connection.State.ToString()=="Open") sqlcmd.Connection.Close();
sqlcmd.Connection.Open();
try
{
sqlcmd.ExecuteNonQuery();
}
catch(Exception e)
{
if (e!=null) errorstr=e.ToString();
}
sqlcmd.Connection.Close();
return errorstr;
}
}
}
===========
调用数据库的方法:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;
using UserData;
namespace LtsClient
{
/// <summary>
/// Form2 的摘要说明。
/// </summary>
public class loginFrm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox shopname;
private System.Windows.Forms.TextBox shoppswd;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button loginbtn;
private System.Windows.Forms.Button cancelbtn;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public loginFrm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.shopname = new System.Windows.Forms.TextBox();
this.shoppswd = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.loginbtn = new System.Windows.Forms.Button();
this.cancelbtn = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(106, 69);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(109, 25);
this.label1.TabIndex = 0;
this.label1.Text = "登录系统";
//
// shopname
//
this.shopname.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
this.shopname.Location = new System.Drawing.Point(114, 103);
this.shopname.MaxLength = 30;
this.shopname.Name = "shopname";
this.shopname.Size = new System.Drawing.Size(109, 21);
this.shopname.TabIndex = 1;
this.shopname.Text = "";
//
// shoppswd
//
this.shoppswd.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
this.shoppswd.Location = new System.Drawing.Point(114, 138);
this.shoppswd.MaxLength = 30;
this.shoppswd.Name = "shoppswd";
this.shoppswd.Size = new System.Drawing.Size(109, 21);
this.shoppswd.TabIndex = 2;
this.shoppswd.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(29, 108);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(77, 17);
this.label2.TabIndex = 3;
this.label2.Text = "分店代号";
//
// label3
//
this.label3.Location = new System.Drawing.Point(29, 140);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(77, 17);
this.label3.TabIndex = 4;
this.label3.Text = "登录密码";
//
// loginbtn
//
this.loginbtn.Location = new System.Drawing.Point(61, 181);
this.loginbtn.Name = "loginbtn";
this.loginbtn.Size = new System.Drawing.Size(83, 25);
this.loginbtn.TabIndex = 5;
this.loginbtn.Text = "连接服务器";
this.loginbtn.Click += new System.EventHandler(this.loginbtn_Click);
//
// cancelbtn
//
this.cancelbtn.Location = new System.Drawing.Point(149, 181);
this.cancelbtn.Name = "cancelbtn";
this.cancelbtn.Size = new System.Drawing.Size(83, 25);
this.cancelbtn.TabIndex = 6;
this.cancelbtn.Text = "取消连接";
this.cancelbtn.Click += new System.EventHandler(this.cancelbtn_Click);
//
// loginFrm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(319, 294);
this.Controls.Add(this.cancelbtn);
this.Controls.Add(this.loginbtn);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.shoppswd);
this.Controls.Add(this.shopname);
this.Controls.Add(this.label1);
this.Name = "loginFrm";
this.Text = "登录服务器";
this.ResumeLayout(false);
}
#endregion
private void loginbtn_Click(object sender, System.EventArgs e)
{
try
{
string UserCode=shopname.Text.Trim();
string UserPswd=shoppswd.Text.Trim();
if(UserCode=="")
{
MessageBox.Show("请输入分店代号");
}
else
{
SqlDataReader usdr=FPara.SqlReader("select top 1 * from DepartMent where UserID='"+UserCode+"'",FPara.connStr);
if(usdr!=null)
{
if(usdr.Read())
{
if(usdr["Pswd"].ToString()!=UserPswd)
{
MessageBox.Show("用户名和密码不相符");
}
else
{
FPara.ShopCode=usdr["UserID"].ToString();
MessageBox.Show("登录成功");
this.Close();
}
}
usdr.Close();
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void cancelbtn_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
===========
问题点数:100、回复次数:7Top
1 楼yuanarea(Sail before)回复于 2006-05-02 12:16:52 得分 20
SQLServer不存在或访问被拒绝
------------------------------
这个信息可能是数据库未启动或网络不通或密码错误,也可能是连接字符串错误
用VS工具栏的服务器资源管理器->数据连接,测试连接成功后,在试.Top
2 楼ice_frank(回归......)回复于 2006-05-02 12:21:40 得分 20
首先还是确认一下数据库是否连接正常Top
3 楼yurow(路漫漫其修远兮,吾将上下而爬楼梯!)回复于 2006-05-02 12:21:45 得分 20
SQLServer不存在或访问被拒绝
可能是SQL未启动,或者连接不对,你尝试打开企业管理器连接数据库后再打开程序试试.如果不成功,那就是连接字符串不对Top
4 楼SaRoot(新风机)回复于 2006-05-02 14:54:54 得分 20
用管理工具
ODBC数据源
看看能不能连通Top
5 楼couxiaozi(臭小子)回复于 2006-05-02 15:29:14 得分 20
string conn = "Server=localhost;uid=sa;pwd=123456;database=goods;";
SqlConnection connStr = new SqlConnection(conn);Top
6 楼ihiptg108()回复于 2006-05-02 21:48:03 得分 0
连接数据库没问题,可能还是语句有问题!Top
7 楼soarnic()回复于 2006-06-16 15:43:04 得分 0
要冷静,
在冷静
最后尝试与电脑进行心灵沟通。Top




