| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5509 人关注过本帖
标题:请教各位如何将我这段代码中的查询到的数据库字段值赋值给字符串变量,以实 ...
只看楼主 加入收藏
huangweia0
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-3-8
结帖率:33.33%
收藏
 问题点数:0 回复次数:8 
请教各位如何将我这段代码中的查询到的数据库字段值赋值给字符串变量,以实现当指定字段为空时插入设定的值。
程序代码:
private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = System.Windows.if (comboBox1.Text == "")
            {
                MessageBox.Show("请选择需要退房的房号,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                string Constr = "server=(local);Integrated Security=true;database=userdb";
                SqlConnection SqlCon = new SqlConnection(Constr);
                SqlCon.Open();
                string Room_id = comboBox1.Text;
                string OutTime = textBox1.Text;
                string LKName = textBox2.Text;
//从这里开始比对退房对话框中所输入的信息与数据库中的信息是否匹配,
//如果匹配并且退房时间为空则可以退房,否则不允许退房。
//但是下面的这些写法似乎在什么地方出了问题了,一时还没找到。截止2010-07-15 5:00.

                string LKNameDb = "select LKname from lkinfo where LKName = '" + LKName + "'";
                string Room_idDb = "select room_id from lkinfo where room_id = '" + Room_id + "'";
                string OutTimeDb = "select outtime from lkinfo";


                if (OutTime == "")
                {
                    MessageBox.Show("请输入退房时间,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (LKName == "")
                {
                    MessageBox.Show("请输入登记入住的旅客姓名,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (LKName == LKNameDb)
                {
                    if (Room_id == Room_idDb)
                    {
                        if (OutTimeDb != "")
                        {
                            MessageBox.Show("您要退的房间本身就是空房间,无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else if (LKName == LKNameDb)
                {
                    if (Room_id == Room_idDb)
                    {
                        if (OutTimeDb == "")
                        {
                            string sqlstr = "update lkinfo set outtime = '" + OutTime + " ' where outtime is null and room_id = '" + Room_id + " ' ";
                            SqlCon.Open();
                            SqlCommand cmd = SqlCon.CreateCommand();
                             = sqlstr;
                            int result = cmd.ExecuteNonQuery();

                            MessageBox.Show("退房成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            comboBox1.Text = "";
                            textBox1.Text = "";
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception ty)
            {
                MessageBox.Show(ty.Message);
            }
        }

请教这段代码本是想通过SQL语句查询数据库中的字段值,并将查询到的字段值赋值给设定字符串变量,然后判断是否OutTime为空,如果为空则插入时间,并提示退房成功。但是搞了很久都没实现。想请教高手,我这段代码什么地方有问题。该如何编写这样功能的代码。
本人没有可用分,所以......哎~~~~~ 惭愧惭愧......

[ 本帖最后由 huangweia0 于 2010-7-18 16:41 编辑 ]
搜索更多相关主题的帖子: 赋值 数据库 字段 变量 字符 
2010-07-17 14:54
liu30211
Rank: 4
来 自:江苏苏州
等 级:业余侠客
威 望:1
帖 子:73
专家分:258
注 册:2008-6-10
收藏
得分:0 
酒店里用的

有做VOD的朋友
联系

VOD播发控件 OSD控件 双屏控件 手写控件
2010-07-17 19:42
huangweia0
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-3-8
收藏
得分:0 
上次在一个旅馆住了N久,他们的旅客登记系统总是没人会搞,经常喊我过去帮忙,我看功能比较单一,后来我就想自己写一个。顺便练练手。

2010-07-18 16:39
huangweia0
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-3-8
收藏
得分:0 
其实我的目的只是想把数据库中真正的字段值查询出来,赋值给我申明的字符串变量,在判断输入框中的值和查询到的值是否相等,但是按照我上面的写法现在出来的结果就是:
LKNameDb的值 = "select LKname from lkinfo where LKName = '输入框中填写好的值'",
而不是:
LKNameDb的值 =输入框中填写好的值
我该怎么修改上面的代码呢?

2010-07-19 16:32
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
收藏
得分:0 
我做了相应的修改,不知道能与你的思路一致:
        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.DropDownStyle = System.Windows.
            if (comboBox1.Text == "")
            {
                MessageBox.Show("请选择需要退房的房号,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            try
            {
                string Constr = "server=(local);Integrated Security=true;database=userdb";
                SqlConnection SqlCon = new SqlConnection(Constr);
                SqlCon.Open();
                string Room_id = comboBox1.Text;
                string OutTime = textBox1.Text;
                string LKName = textBox2.Text;

                 string sqlstr="";
                 if (OutTime == "")
                 {
                     MessageBox.Show("请输入退房时间,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     textBox1.Focus();
                     return;
                 }
                     
                 if (LKName == "")
                 {
                     MessageBox.Show("请输入登记入住的旅客姓名,否则无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     textBox2.Focus();
                     return;
                 }

                 string sqlstr="select outtime from lkinfo where LKname='"+LKName+"' and room_id='"+Room_id+"'";
                 //执行数据库查询...省略
                 
                 if(查询结果不为零)
                 {
                     if (字段[outtime]值 != "")
                     {
                        MessageBox.Show("您要退的房间本身就是空房间,无法退房!", "警告!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                     }
                     else
                     {
                        string sqlstr = "update lkinfo set outtime = '" + OutTime + " ' where outtime is null and room_id = '" + Room_id + " ' ";
                        SqlCon.Open();
                        SqlCommand cmd = SqlCon.CreateCommand();
                         = sqlstr;
                        int result = cmd.ExecuteNonQuery();

                        MessageBox.Show("退房成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        comboBox1.Text = "";
                        textBox1.Text = "";
                        this.Close();
                     }
                 }
                 else
                 {
                    //从你的程序上好像没有这块的处理
                    //希望再从逻辑上处理这块
                 }
            }
            catch (Exception ty)
            {
                MessageBox.Show(ty.Message);
            }
        }
2010-07-20 10:15
hainandgm
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:111
专家分:475
注 册:2007-6-21
收藏
得分:0 
不好意思,多写了一个"string sqlstr=""",把这个删除
2010-07-20 10:17
huangweia0
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-3-8
收藏
得分:0 
谢谢,我准备测试一下你修改的程序。

2010-07-29 10:25
Anne_Zhang
Rank: 2
等 级:论坛游民
威 望:1
帖 子:27
专家分:94
注 册:2010-4-10
收藏
得分:0 
{string LKNameDb = "select LKname from lkinfo where LKName = '" + LKName + "'";
                string Room_idDb = "select room_id from lkinfo where room_id = '" + Room_id + "'";
                string OutTimeDb = "select outtime from lkinfo";
}
你的查询语句有问题,应该改为:string sql="select outtime from lkinfo where LKName='" + LKName + "' and room_id = '" + Room_id + "'";
执行查询后得到outtime时间然后进行比较
2010-07-29 16:55
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
程序代码:
string Room_id = comboBox1.Text;
string OutTime = textBox1.Text;
string LKName = textBox2.Text;
try
{
    string Constr = "server=(local);Integrated Security=true;database=userdb";
    SqlConnection SqlCon = new SqlConnection(Constr);
    string sqlstr = "select * from lkinfo where LKName = '" + LKName + "' and room_id = '" + Room_id + "' and outtime is null";
              
    SqlDataAdapter da = new SqlDataAdapter(sqlstr, SqlCon);
    DataSet ds = new DataSet();
    da.Fill(ds, "lkinfo");
    if(ds.Tables["lkinfo"].Rows.Count == 0)   //没有数据
    {
        MessageBox.Show("您要退的房间本身就是空房间,无需退房!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        return;
    }
    ds.Tables["lkinfo"].Rows[0]["outtime"] = DateTime.Now.Tostring("yyyy-MM-dd HH:mm:ss");
    da.Update(ds, "lkinfo");

    MessageBox.Show("退房成功!", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    comboBox1.Text = "";
    textBox1.Text = "";
}
catch (Exception ty)
{
    MessageBox.Show(ty.Message);
}

记事本打的,自己再调试一下。

[ 本帖最后由 bygg 于 2010-7-29 18:19 编辑 ]

飘过~~
2010-07-29 18:16
快速回复:请教各位如何将我这段代码中的查询到的数据库字段值赋值给字符串变量, ...
数据加载中...
 
   



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

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