基于VS2005的,数据库连接

1 sql2000数据库小图标需要是绿颜色
2 打开企业管理器
3 新建数据库Test1
4 建立表teacher_table (TID,TName,TPassword)
5 修改sa 密码
6 创建一个winform 程序
7 画界面
8 参考代码,执行数据库操作。
9 测试运行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

//访问SQL 数据库都需要如下引用
using System.Data;
using System.Data.SqlClient;


namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //验证教师姓名和密码
            //获取教师编号
            string str_id= textBox1.Text.Trim();
            //获取教师姓名
            string str_name = textBox2.Text.Trim();
            //获取教师密码
            string str_password = textBox3.Text.Trim();
            //
            //MessageBox.Show("你输入的教师编号是:" + str_id + "/r 你输入的教师姓名是:" + str_name + "/r 你输入的教师密码是:" + str_password);
            //到数据库Test的teacher_table 验证用户名和密码是否正确
          
            try
            {
                //设置数据库访问字符串 Test1 是数据库名称 uid 设置用户名 pwd 设置密码
                String connstr = "server=.;database=Test1;uid=sa;pwd=111111;Max Pool Size=20000;";
                //数据库连接
                SqlConnection conn = new SqlConnection(connstr);
                //测试连接是否正常
                conn.Open();
                //MessageBox.Show("数据库连接正常");

                //使用命令访问数据库里面的表
                SqlCommand cmd = new SqlCommand();
                //SqlCommand 数据库连接设置
                cmd.Connection = conn;
                string str_SQL="select * from dbo.teacher_table where TID='"
                    + str_id + "' and TName='"
                    + str_name + "' and TPassword='"
                    + str_password + "'";
                //传递SQL语句
                cmd.CommandText = str_SQL;


                //建立数据库集
                DataSet ds = new DataSet();
                //建立数据库适配器
                SqlDataAdapter ad = new SqlDataAdapter();
                //适配器绑定SQLCommand
                ad.SelectCommand = cmd;
                //通过数据库适配器填充数据集
                ad.Fill(ds);

                if (ds.Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("你输入的用户编号,姓名,密码是正确的");
                }
                else
                {
                    MessageBox.Show("你输入的用户编号,姓名,密码是错误的");
                }


                conn.Close();
            }
            catch (Exception exc)
            {
                MessageBox.Show("数据库连接异常");
            }

           
        }

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

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值