标题:[求助]c#连接SQLSERVER登陆出问题了
只看楼主
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
 问题点数:0 回复次数:17 
[求助]c#连接SQLSERVER登陆出问题了

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

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 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.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.textBox2.Location = new System.Drawing.Point(208, 176);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.Font = new System.Drawing.Font("新宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.ForeColor = System.Drawing.SystemColors.InactiveCaption;
this.button1.Location = new System.Drawing.Point(128, 248);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "登陆(&L)";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.SystemColors.Control;
this.button2.Font = new System.Drawing.Font("新宋体", 9F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button2.ForeColor = System.Drawing.SystemColors.InactiveCaption;
this.button2.Location = new System.Drawing.Point(240, 248);
this.button2.Name = "button2";
this.button2.TabIndex = 3;
this.button2.Text = "退出(&E)";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.Font = new System.Drawing.Font("新宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.SystemColors.InactiveCaption;
this.label1.Location = new System.Drawing.Point(104, 96);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "用户名";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.Control;
this.label2.Font = new System.Drawing.Font("新宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.ForeColor = System.Drawing.SystemColors.InactiveCaption;
this.label2.Location = new System.Drawing.Point(104, 176);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "密码";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this.label3.Location = new System.Drawing.Point(120, 24);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(216, 32);
this.label3.TabIndex = 6;
this.label3.Text = "公司名称";
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.textBox1.Location = new System.Drawing.Point(208, 96);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 7;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(520, 349);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "欢迎登陆";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

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

private void Form1_Load(object sender, System.EventArgs e)
{

}

private void button1_Click(object sender, System.EventArgs e)
{

//验证信息
if(this.textBox1.Text==""||this.textBox2.Text=="")
{
MessageBox.Show("账号或密码不能为空!");
this.textBox1.Focus();
}

string sql="select * from Land where Admin_UserName='"+textBox1.Text.Trim()+"'and Admin_Passwd='"+textBox2.Text.Trim()+"'";
SqlConnection sqlcon = new SqlConnection("server =(Local);uid = sa; pwd =; database =TL01");
SqlCommand com=new SqlCommand(sql,sqlcon);
sqlcon.Open();
SqlDataReader sdr=com.ExecuteReader();


if(sdr.Read())
{
Form2 form2=new Form2();
form2.Show();
}
}


private void button2_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
}
}




上面是登陆代码 实现的功能就是填入用户名和密码,然后直接转到Form2,我不知道哪里错了,点登陆,提示我(未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 system.data.dll 中。

其他信息: 系统错误。) 调试中说"红字"的地方有问题~~~我实在搞不明白为什么??请斑竹和各位达人帮帮小弟忙,感激不尽~在线等~!!1

搜索更多相关主题的帖子: SQLSERVER 登陆 
2007-10-11 14:20
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 
没有办法转到FORM2.....也无法实现登陆....头都疼了 原本以为不是什么大问题~~但是忙了一上午没有搞定~~`拜托大家 帮忙帮忙解决哈~~谢谢谢谢

我承认~~我就是菜鸟!!!
2007-10-11 14:23
crazymk
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2007-8-30
得分:0 
sql異常 看看表名字段名有沒有錯誤吧
最好把sql錯誤碼分析一下

すばらしいコードを書き出すのは楽しい事です
2007-10-11 14:25
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 

什么是SQL错误码??


我承认~~我就是菜鸟!!!
2007-10-11 14:27
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 
SQL里面的字段就两个 Admin_UserName  Admin_Passwd  我全部都从数据库里复制过来的...不会错啊

我承认~~我就是菜鸟!!!
2007-10-11 14:30
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 
字段类型也是最常用VARCHAR~~.....

我承认~~我就是菜鸟!!!
2007-10-11 14:31
crazymk
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2007-8-30
得分:0 
加個try catch 把異常捕獲,把錯誤碼發出來。

すばらしいコードを書き出すのは楽しい事です
2007-10-11 14:33
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 
try catch??加在哪里??麻烦指示下好吗??我实在不是很清楚...谢谢

我承认~~我就是菜鸟!!!
2007-10-11 14:34
crazymk
Rank: 1
等 级:新手上路
威 望:1
帖 子:343
专家分:0
注 册:2007-8-30
得分:0 
哪裡出錯就加哪裡丫。
try
{
SqlDataReader sdr=com.ExecuteReader();
}
catch (Exception ex)
{

}

すばらしいコードを書き出すのは楽しい事です
2007-10-11 14:40
zhuxian713
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-10-8
得分:0 

加了以后 新错误产生了...

private void button1_Click(object sender, System.EventArgs e)
{

//验证信息
if(this.textBox1.Text==""||this.textBox2.Text=="")
{
MessageBox.Show("账号或密码不能为空!");
this.textBox1.Focus();
}

string sql="select * from Land where Admin_UserName='"+textBox1.Text.Trim()+"'and Admin_Passwd='"+textBox2.Text.Trim()+"'";
SqlConnection sqlcon = new SqlConnection("server =(Local);uid = sa; pwd =; database =TL01");
SqlCommand com=new SqlCommand(sql,sqlcon);
sqlcon.Open();
try
{
SqlDataReader sdr=com.ExecuteReader();
}
catch(Exception ex)
{
}

if(sdr.Read())
{
Form2 form2=new Form2();
form2.Show();
}
}

找不到类型或命名空间名称“sdr”(是否缺少 using 指令或程序集引用?)

红字的地方出错~~


我承认~~我就是菜鸟!!!
2007-10-11 14:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-176724-1-1.html




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

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