标题:简单的登陆界面,无法把信息插入数据库。
只看楼主
wuweitiandia
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-1-27
结帖率:0
已结贴  问题点数:20 回复次数:7 
简单的登陆界面,无法把信息插入数据库。
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
winners
Rank: 6Rank: 6
来 自:济南
等 级:侠之大者
威 望:1
帖 子:105
专家分:416
注 册:2009-3-20
得分:3 
你的mycmd没执行ExecuteNonQuery()方法,再一个是你的SQL语句有问题,应该是Values ('"+Text1.Text+"','"+Text2.Text+"'")

[ 本帖最后由 winners 于 2012-1-27 20:13 编辑 ]
2012-01-27 20:02
yinniannian
Rank: 9Rank: 9Rank: 9
来 自:河北省石家庄
等 级:蜘蛛侠
威 望:2
帖 子:256
专家分:1007
注 册:2011-5-13
得分:3 
同意楼上

代做小型软件。
QQ:449795473
2012-01-30 11:02
BigPei
Rank: 3Rank: 3
来 自:苏州
等 级:论坛游侠
威 望:6
帖 子:43
专家分:161
注 册:2012-1-10
得分:3 
string upd = "Insert into Table1(username,password) values('" + textBox1.Text + "' ,'" + textBox2.Text + "')";
mycmd.ExecuteNonQuery();
使用存储过程吧,不要再程序中直接使用SQL语句

Fighting forever.
2012-01-30 17:13
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:3 
别的问题也很多唉 都是新人常犯的毛病 还得多练习啊
别的问题也很多唉 都是新人常犯的毛病 还得多练习啊
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)); //参数名不规范 如果textBox1.Text如果有空格呢 trim()!
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            string pw = reader.GetString(reader.GetOrdinal("password"));
                            if (pw == textBox2.Text) //pwd
                            {
                                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')"; //这么写out 啦
                 
                   SqlCommand mycmd = new SqlCommand(upd,con);
                    MessageBox.Show("注册成功!!");
                //在这里数据库连接为什么不关闭? 释放?
            }
        }
    }
}


[ 本帖最后由 wangnannan 于 2012-1-31 09:55 编辑 ]

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2012-01-31 09:48
girl0001
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:118
注 册:2010-7-17
得分:3 
楼上分析的很详细。。谢谢
2012-01-31 11:46
BigPei
Rank: 3Rank: 3
来 自:苏州
等 级:论坛游侠
威 望:6
帖 子:43
专家分:161
注 册:2012-1-10
得分:0 
回复 5楼 wangnannan
V5
学习了

Fighting forever.
2012-01-31 15:12
sdjyld
Rank: 1
等 级:新手上路
帖 子:8
专家分:5
注 册:2011-5-24
得分:3 
学习了
2012-02-01 15:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-360048-1-1.html




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

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