高手帮下忙啊,我要做毕业实际啊!

kenjiwilly 2009-03-04 10:56:10
using System;
using System.Collections.Generic;
namespace 学生管理系统
{
public partial class Frm_登陆界面 : Form
{
public Frm_登陆界面()
{
InitializeComponent();
skinEngine1.SkinFile = "SteelBlue.ssk";
}
private void btnCancel_Click_1(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("确定要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
Application.Exit();
}

private void btnLogin_Click(object sender, EventArgs e)
{
Frm_主界面 Fm = new Frm_主界面();
MyData mydata = new MyData();
string s = "select * from [user] where usrno='" + this.txtusername.Text + "' and usrpwd ='" + this.txtstuPSW.Text + "'and ustype='" + comboBox1.Text + "'";
SqlDataReader dr = mydata.getDataReader(s);
if (txtusername.Text == "")
{
MessageBox.Show("请输入您的用户名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (txtstuPSW.Text == "")
{
MessageBox.Show("请输入密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (comboBox1.Text == "")
{
MessageBox.Show("请选择您的身份!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (dr.HasRows)
{
while (dr.Read())
{
string strusername = dr["usrno"].ToString().Trim();
string username = this.txtusername.Text;
string comtype = dr["ustype"].ToString().Trim();
if (username == strusername)
{
string stuPSW = dr["usrpwd"].ToString().Trim();
string psw = this.txtstuPSW.Text;
if (stuPSW == psw)
{
if (comtype == this.comboBox1.Text)
{
Fm.UserName = strusername;
Fm.PassWord = stuPSW;
Fm.UserType = comtype;
Fm.Show();
txtusername.Text = "";
txtstuPSW.Text = "";
comboBox1.Text = "";
this.Hide();
}

}
}
}
}
else
{
MessageBox.Show("对不起,你的密码、用户名或身份错误!\n 请重新输入!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtusername.Text = "";
txtstuPSW.Text = "";
comboBox1.Text = "";
txtusername.Focus();
}
mydata.closeConnection();
}

大家帮我看看这段代码哪儿错了
...全文
88 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
BEN254422571 2009-03-04
  • 打赏
  • 举报
回复
设计都不合理·都有问题~~~~~

应该写个公共处理数据的类吧~~~
// ================设计说明=================
// 但前类名称:dao.java
// 设计目的: 数据连接处理
// 设计时间:2009-1-14
// ========================================

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class dao {

private Connection conn;

private PreparedStatement pstmt;

private ResultSet re;

// ---------------------
// 获取Connection连接对象
// ---------------------
public Connection getConn()throws NamingException,SQLException
{

Context ic=new InitialContext();
DataSource source= (DataSource)ic.lookup("java:comp/env/jdbc/message");
conn=source.getConnection();
return conn;

}

// ---------------------
// 关闭资源
// ---------------------
public void closeAll()
{
try{
if(re!=null)
{
re.close();
}
}catch(SQLException e)
{
System.out.println("关闭ResultSet对象资源错误!");
e.printStackTrace();
}

try{
if(pstmt!=null)
{
pstmt.close();
}
}catch(SQLException e1)
{
System.out.println("关闭PreparedStatement对象资源错误!");
e1.printStackTrace();
}

try{
if(conn!=null&&(!conn.isClosed()))
{
conn.close();
}
}catch(SQLException e2)
{
System.out.println("关闭Connection对象资源错误!");
e2.printStackTrace();
}

}

// ---------------------
// 执行增删改通用
// ---------------------
public boolean execute(String sql,Object[] element)throws NamingException,SQLException
{
boolean bool = false;
try{
conn=this.getConn(); //获取连接
pstmt = conn.prepareStatement(sql);
if(element!=null) //遍历元素
{
for(int i=0;i <element.length;i++)
{
pstmt.setObject((i+1),element[i]);
}
}
if (pstmt.executeUpdate() > 0)
{
bool = true;
}

} finally {
this.closeAll(); //关闭资源
}

return bool;
}

// ---------------------
// 执行查询通用
// ---------------------
public ResultSet executeQuery(String sql,Object[] element)throws NamingException,SQLException
{

conn=this.getConn(); //获取连接
pstmt = conn.prepareStatement(sql);
if(element!=null) //遍历元素
{
for(int i=0;i <element.length;i++)
{
pstmt.setObject((i+1),element[i]);
}
}
re= pstmt.executeQuery(); //执行查询操作
return re;
}

// ---------------------
// 执行查询是否存在通用
// ---------------------
public Boolean executeQueryBy(String sql,Object[] element)throws NamingException,SQLException
{

try{
conn=this.getConn(); //获取连接
pstmt = conn.prepareStatement(sql);
if(element!=null) //遍历元素
{
for(int i=0;i <element.length;i++)
{
pstmt.setObject((i+1),element[i]);
}
}
re=pstmt.executeQuery();
if(re.next()) //查看结果集是否存在元素
{
return true;
}else
{
return false;
}

} finally {
this.closeAll(); //关闭资源
}

}

}

pztx1992 2009-03-04
  • 打赏
  • 举报
回复
中文都搞出来了....
quietspring 2009-03-04
  • 打赏
  • 举报
回复
SqlDataReader dr=null;
dr = mydata.getDataReader(s);
贫僧又回来了 2009-03-04
  • 打赏
  • 举报
回复
像string strusername = dr["usrno"].ToString().Trim();这样的地方断点看下是不是有空值!
kenjiwilly 2009-03-04
  • 打赏
  • 举报
回复
说是:未将对象引用设置到对象的实例
bigcoder 2009-03-04
  • 打赏
  • 举报
回复
没有和数据库连接呀
kenjiwilly 2009-03-04
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;

namespace 学生管理系统
{
public class MyData
{
private string strDSN; //定义数据库连接对象
private SqlConnection myConn;
public MyData() // 构造函数实现打开数据库连接
{
strDSN = "Data Source=.;Initial Catalog=StudentManage;Integrated Security=True";
myConn = new SqlConnection(strDSN);
myConn.Open();
}

//利用DataReader对象检索数据
public SqlDataReader getDataReader(String strSQL)
{
try
{
SqlCommand myCmd = new SqlCommand(strSQL, myConn);
SqlDataReader datareader = myCmd.ExecuteReader();
return datareader;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "数据库操作失败!", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}

//更新数据库
public Boolean updateSql(string strSQL)
{
SqlCommand myCmd = new SqlCommand(strSQL, myConn);
try
{
myCmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{

MessageBox.Show(e.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
}

//创建数据集
public DataSet getDataSet(string str,string table)
{
SqlDataAdapter da = new SqlDataAdapter(str, myConn);
DataSet ds = new DataSet();
da.Fill(ds, table);
return ds;
}

//关闭数据库连接
public void closeConnection()
{
myConn.Close();

}

}
}
数据库在这
kenjiwilly 2009-03-04
  • 打赏
  • 举报
回复
我用C#啊,不是JAVA啊

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧