为什么会出现connection没有初始化的错误提示?
问题如题目…………
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace student
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnYes_Click(object sender, EventArgs e)
{
string userName = txtName.Text;
string password = txtPwd.Text;
string conString = "Server=.;User ID=sa;Pwd=123;DataBase=student";
SqlConnection connection = new SqlConnection(conString);
string sql = string.Format("select count(*) from [User] where userName='{0}' and password='{1}'",userName,password);
try
{
connection.Open();
SqlCommand command = new SqlCommand();
int num = (int)command.ExecuteScalar();
if (num > 0)
{
MessageBox.Show("欢迎进入成绩管理系统!", "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
MainForm mainForm = new MainForm();
this.Visible = false;
}
else
{
MessageBox.Show("输入的用户名或者密码错误!", "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"操作数据库出错!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{connection.Close();}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace student
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void btnYes_Click(object sender, EventArgs e)
{
string userName = txtName.Text;
string password = txtPwd.Text;
string conString = "Server=.;User ID=sa;Pwd=123;DataBase=student";
SqlConnection connection = new SqlConnection(conString);
string sql = string.Format("select count(*) from [User] where userName='{0}' and password='{1}'",userName,password);
try
{
connection.Open();
SqlCommand command = new SqlCommand();
int num = (int)command.ExecuteScalar();
if (num > 0)
{
MessageBox.Show("欢迎进入成绩管理系统!", "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
MainForm mainForm = new MainForm();
this.Visible = false;
}
else
{
MessageBox.Show("输入的用户名或者密码错误!", "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,"操作数据库出错!",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
finally
{connection.Close();}
}
}
}
...全文
相关推荐
android判断phonegap是否联网且加载super.loadUrl网址
android判断phonegap是否联网动态加载super.loadUrl网址,接下来本文所提供的知识会帮助你解决以上问题,感兴趣的你可不要错过了哈
Android文件下载进度条的实现代码
我们今天开始学习的是下载进度的实现。今天的这段代码是网上找的,自己做了些小改,通过模拟器测试。文件下载进度条控制(就是为了高清壁纸加个进度条),自己研究了好久,但是进度条只能显示缓存写入文件的进度,不能显示下载进度。找了好久,终于找到一段用的代码,所以记录下来,大家分享
Android实现下载zip压缩文件并解压的方法(附源码)
主要给大家介绍了利用Android实现下载zip压缩文件并解压的方法,文中给出了示例代码并提供了源码下载,需要的朋友可以参考借鉴,下面来一起看看吧。
eclipse下搭建hibernate5.0环境的步骤(图文)
主要介绍了eclipse下搭建hibernate5.0环境的步骤(图文),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Android中实现下载和解压zip文件功能代码分享
主要介绍了Android中实现下载和解压zip文件功能代码分享,本文直接给出了实现代码,需要的朋友可以参考下


