我想逐行更新leftdays ..row ..不是一次全部。 [英] I want to update the leftdays ..row by row .. Not all at once .

查看:100
本文介绍了我想逐行更新leftdays ..row ..不是一次全部。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

..我怎么能这样做..请看下面的代码并注释该更新声明的代码部分..

.. how i can do that .. please watch my code below and comment the code portion for that update statement ..

<pre lang="c#">private void button3_Click(object sender, EventArgs e)
        {

            myconnection.Open();
            displayData();
            string selectSql = "select * from employTable2";
            SqlCommand com = new SqlCommand(selectSql, myconnection);
           using (SqlDataReader read = com.ExecuteReader())
            {
                while (read.Read())
                {
                    string first1 = read["salarydate"].ToString();
                    string Second1 = read["expiredate"].ToString();
                     TimeSpan t1 = Convert.ToDateTime(Second1) - DateTime.Now;
                    int daysleft = t1.Days;
                    SqlConnection myconnection1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\muhammad\my documents\visual studio 2010\Projects\ProjectClock\ProjectClock\ClockDB.mdf;Integrated Security=True;User Instance=True;");
                    myconnection1.Open();
                    SqlCommand cmd1 = new SqlCommand("UPDATE employTable2 SET daysleft='" + daysleft.ToString() + "'", myconnection1);
                   
                    cmd1.ExecuteNonQuery();
                    myconnection1.Close();
                  MessageBox.Show(t1.Days +"", "Days Left");
                }
                myconnection.Close();

            } 
        }





我的尝试: < br $>


i尝试更新数据库表中的leftdays列..但它一次更新所有..带有通过datareader循环的最后一个值..我想更新它一行一行..我怎么能这样做请帮助...评论我那部分的代码..



What I have tried:

i tried to update the leftdays column in DB table .. but its updates all at once .. with last value that come through loop of datareader .. i want to update it row by row .. how i can do that please help... comment me code for that portion ..

推荐答案

为什么?为什么要存储每天都在变化的值,并且可以从某些常量(例如expiredate)中轻松导出?这就像一个人的年龄,你只需要存储他的出生日期,随时知道他的年龄,你不这么认为吗?
What for? Why do you want to store a value that is changing every day and can be easily derivable from some constants such as the expiredate? It is like the age of a person, you just need to store his date of birth to know his age at any time, don't you think so?


Peter Leow是对的,不过如果你想逐行执行,你需要在UPDATE命令中添加一个WHERE语句,只更新你刚才读的记录。



你也可以使用SqlConnection .Clone复制连接设置而不是重复连接字符串,你仍然需要打开和关闭克隆,因为它只复制来自源的设置。



Peter Leow is right, however if you want to do it row by row you will need to add a WHERE statement to the UPDATE command to only update the record you just read.

You can also use the SqlConnection .Clone to copy the connection settings rather than repeat your connection string, you still have to open and close the clone as it only copies the settings from the source.

SqlConnection myconnection1 = myconnection.Clone()





你仍然在使用myconnection.close在使用之外的.open中,有一天你将尝试调试一段代码,想知道为什么它在一切正常时失败,保持匹配的代码行在同一级别有助于防止未来的错误。



You still have the myconnection.close inside the using with the .open outside the using, one day you will be trying to debug a piece of code wondering why it fails when everything looks right, keeping matching lines of code at the same level helps prevent future bugs.


这篇关于我想逐行更新leftdays ..row ..不是一次全部。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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