如何减少Sql的数据? [英] How Can I Reduce Data Of Sql?

查看:133
本文介绍了如何减少Sql的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我的朋友们。

在我的数据库表中,我有一个字段,它的名字是数。

在我的程序中,我有一个按钮。

当我点击按钮时,我想从数据库中的字段[count]减少一个数字。

例如,字段[count]的值是5.
当我点击按钮时,
i想要的值变为4。



我该怎么做?

解决方案

< blockquote>

 更新 MyTable 
设置 MyCount =(MyCount - 1
WHERE MyTableID = 23


Missng一些细节,但假设你的意思是在sql中它可能是



  update  mytable 
set [count] = [count] - 1





但是使用a的原因有点奇怪table for,因为一个表只有一行?

其他明智的是必须有一个关键列以及在表格中定义列[count]应该减少的行。



如果来自C#而不是,因为问题上的标记你可以发出什么system.data.sqlclient命名空间的命令



  public  < span class =code-keyword> static   void  DecrementMyTable()
{
using var cn = new System.Data.SqlClient.SqlConnection( myconnectionstring))
{
使用 var cmd = new System.Data.SqlClient.SqlCommand( 我的更新查询),cn)
{
md.ExecuteNonQ uery();
}
}
}





然后您可以调用类似于您的方法的方法按钮'onclick'事件,假设它已将runat设置为'server'


在按钮上单击两次,它将向您发送带有空白事件的.cs文件。将此代码粘贴到该事件中:



 尝试 
{
SqlConnection con = new SqlConnection( yourconnectionstringhere);
con.Open();

SqlCommand cmd = new SqlCommand( UPDATE [nameofthetable] SET [Count] = [Count] - 1 WHERE ID = @ID,con);
// 编辑:防止sql注入
cmd.Parameters.Add( new SqlParameter( @ ID, txtIDFrom.Text));
cmd.ExecuteNonQuery();
}
catch (例外情况)
{
throw ex;
}
最后
{
con.Close();
}


hi my friends.
in my table of database , i have field that it name is count.
in my program , i have a button.
when i click button , i want reduce one number from field [count] in database.
for example , value of field [count] is 5.
i want value become 4 when i click button.

how can i do that?

解决方案

Update MyTable 
Set MyCount = (MyCount - 1)
WHERE MyTableID = 23


Missng a few details, but presume you mean in sql it could be

update mytable
set [count] = [count] - 1



but that of cause is a bit odd to use a table for, because a table with just one row?
other wise there must be a key column as well to define the row in your table where there column [count] should be reduced.

if from C# instead, because of the tag on the question you could issue whatever command you want from system.data.sqlclient namespace

public static void DecrementMyTable()
{
    using (var cn = new System.Data.SqlClient.SqlConnection("myconnectionstring"))
    {
       using (var cmd = new System.Data.SqlClient.SqlCommand("my update query"), cn)
         {
              md.ExecuteNonQuery();
         }
     }
}



You could then call a method like the outlined one on your button 'onclick' event, presuming it has runat set to 'server'


click twice on your button and it will send you to .cs file with a blank event. Paste this code into that event:

            try
            { 
                SqlConnection con = new SqlConnection("yourconnectionstringhere");
                con.Open(); 

                SqlCommand cmd = new SqlCommand("UPDATE [nameofthetable] SET [Count] = [Count] - 1 WHERE ID = @ID", con);
//EDIT: protects against sql injection
                cmd.Parameters.Add(new SqlParameter("@ID", txtIDFrom.Text));
                cmd.ExecuteNonQuery(); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }


这篇关于如何减少Sql的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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