使用SQL查询进行C#数据更新 [英] C# data update using SQL queries

查看:60
本文介绍了使用SQL查询进行C#数据更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
            {
                DBconnection db = new DBconnection();
                db.SqlQuery("Select * from Users_Data Where Email = @em");
                db.cmd.Parameters.AddWithValue("em", textBox1.Text);
                db.ExQuery();
                SqlDataReader dr = db.cmd.ExecuteReader();

                if (dr.Read())
                {

                    db.SqlQuery("Update Users_Data set LoginTimer = " + 1 + "Where Email = @email");
                    db.cmd.Parameters.AddWithValue("email", textBox1.Text);
                    MessageBox.Show("Updated Sucessfully");
                    //logintimer = Convert.ToInt32(textBox1.Text.ToString())   ;
                    //logintimer.return(drr["LoginTimer"].ToString());

                    //dr["Email"].ToString().Contains(textBox1.Text);
                    if (dr["Email"].ToString().Contains(textBox1.Text))
                    {

                        logintimer = Convert.ToInt32(dr["LoginTimer"].ToString());
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("" + ex);

            }

            if (logintimer == 1)
            {
                string message = "Plz change your password to continue";
                string title = "Criteria";
                MessageBoxButtons mbb = MessageBoxButtons.YesNo;
                DialogResult result = MessageBox.Show(message, title, mbb);
                if (result == DialogResult.Yes)
                {
                    UserProfile up = new UserProfile(textBox1.Text);
                    up.Show();
                    this.Hide();

                }
                else
                {
                    UserForm uf = new UserForm();
                    uf.Show();
                    this.Hide();
                }
            }
        }





我尝试了什么:



i想要更新记录并在用户登录时通过一个登录计数器递增但在我的数据库表中它增加1并且不从一个人增加以上某人plz帮助我out with my code



What I have tried:

i want to update the record and increment by one login counter whenever user signin but in my database table it is incremented by one and not incrementing above from one someone plz help me out with my code

推荐答案

看起来你的update语句总是设置为1
It looks like your update statement always sets to 1
db.SqlQuery("Update Users_Data set LoginTimer = " + 1 + "Where Email = @email");

哪个应转换为

UPDATE Users_Data
SET LoginTimer = 1
WHERE Email = @email





从你的问题和阅读你的代码;似乎你打算做的是每次登录时增加值,如下所示:



From your question and reading your code; it seems that what you are intending to do is to increment the value every time you login, something like this:

UPDATE Users_Data
SET LoginTimer = LoginTimer + 1
WHERE Email = @email

现在回到C#....请注意,这里不需要字符串连接(或者在原始版本)

Back into C# now.... Notice you do not need the string concatenation here (or in the original version)

db.SqlQuery("Update Users_Data set LoginTimer = LoginTimer + 1 Where Email = @email");


实际上,您的SQL查询如下所示:

Actually, your SQL query looks like this:
Update Users_Data set LoginTimer = 1Where Email = @email



你需要1和WHERE子句之间的空格。



您也不需要将1分为数值。它只能是没有+运算符的字符串的一部分。


You need a space between the 1 and WHERE clause.

You also don't need to break the 1 out into a numerical value. It can just be part of the string without the + operators.

db.SqlQuery("UPDATE Users_Data SET LoginTimer = 1 WHERE Email = @email");


这篇关于使用SQL查询进行C#数据更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆