日期:2014-05-17  浏览次数:20715 次

c#中如何用SqlDataReader提取数据库中的数据?(求解答)


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 记账系统2
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtName.Text == ""||txtPwd.Text == "")//判断用户名是否为空
            {
                MessageBox.Show("请输入用户名或密码!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            else
            {
                    try
                    {

                        this.sqlConnection1.Open();//打开数据库连接
                        this.sqlCommand1.Parameters["@username"].Value = this.txtName.Text;//赋值给sql语句中@username
                        this.sqlCommand1.Parameters["@password"].Value = this.txtPwd.Text;
                        SqlDataReader reader = this.sqlCommand1.ExecuteReader() ;
                        int i = int.Parse(this.sqlCommand1.ExecuteScalar().ToString());//查询并返回查询结果集中第一行的第一列忽略其他列                    
                        if (i>0)
                        { 
                            MainForm mform = new MainForm();