| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1123 人关注过本帖
标题:简单的登陆界面,无法把信息插入数据库。
取消只看楼主 加入收藏
wuweitiandia
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-1-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:0 
简单的登陆界面,无法把信息插入数据库。
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        private void button1_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(
@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                     = "Select * from Table1 where username=@un";
                    cmd.Parameters.Add(new SqlParameter("un", textBox1.Text));
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            string pw = reader.GetString(reader.GetOrdinal("password"));
                            if (pw == textBox2.Text)
                            {
                                MessageBox.Show("登陆成功!");
                            }
                            else
                            {
                                MessageBox.Show("登录失败!");
                            }
                        }
                        else
                        {
                            MessageBox.Show("用户名不存在!");
                        }
                    }
                }
            }




        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(
@"Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|\mydb.mdf;Integrated Security=True;User Instance=True"))
            {
                con.Open();
      
                  
                   string upd = "Insert into Table1(username,password) values('textBox1.Text' ,'textBox2.Text')";
                 
                   SqlCommand mycmd = new SqlCommand(upd,con);
                    MessageBox.Show("注册成功!!");
               
            }
        }
    }
}


说明:快速注册的功能是向数据库插入数据,但是调试之后去数据库查看,并没有插入新数据。编译没有出错~
搜索更多相关主题的帖子: public private class 数据库 
2012-01-27 15:47
快速回复:简单的登陆界面,无法把信息插入数据库。
数据加载中...
 
   



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

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