如何获取sql表中字段的值,然后将其用作变量?

时间:2015-07-21 15:48:22

标签: c# sql

我想知道是否有任何方法可以直接获取sql表中字段的值,所以我可以将它用作C#中的变量。我打算用它作为柜台的起始值:

  private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("A Apagar dados");
        bool check = true;
        do
        {
            string connectionString = @"Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";
            string queryString = string.Empty;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                queryString = "DELETE FROM wgcdoccab WHERE serie ='1'and tipodoc ='FSS'  and contribuinte ='999999990' and numdoc=  numerador and datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120) ";

                SqlCommand command = new SqlCommand(queryString, connection);
                //command.Connection.Open();
                command.ExecuteNonQuery();

            }
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                queryString = "SELECT * FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS'  and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120) ";

                using (SqlCommand command = new SqlCommand(queryString, connection))
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        check = true;
                        numerador=numerador-1;
                    }
                    else
                    {
                        check = false;
                        MessageBox.Show("Dados Apagados com sucesso");
                    }
                    command.Connection.Close();
                }
            }
        }
        while (check);

我知道代码不起作用,我改变了所以我可以更好地解释我想要实现的目标,基本上我需要numdoc的值,所以我以后可以用它作为计数器的启动器我将在以后这一行减少:

                            check = true;
                        numerador=numerador-1;

如果我没有正确解释这一点,我很抱歉,我的母语不是英语。

1 个答案:

答案 0 :(得分:1)

编辑:

不是返回包含行的结果集来迭代,而是只使用MIN()来获得WHERE子句的最低数量。

        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            connection.Open();
            queryString = "SELECT MIN(ISNULL(numdoc,0)) FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS'  and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120) ";

            using (SqlCommand command = new SqlCommand(queryString, connection))
            {
                int num= Convert.ToInt32( command.ExecuteScalar());
                if (num> 0)
                {
                    check = true;
                    numerador=num;
                }
                else
                {
                    check = false;
                    MessageBox.Show("Dados Apagados com sucesso");
                }
                command.Connection.Close();
            }
        }

但是,如果您想获得像MAX()这样的价值,那么您可以这样做:

queryString = "SELECT MAX(ISNULL(column_name,0)) FROM wgcdoccab WHERE serie ='1' and tipodoc ='FSS'  and contribuinte ='999999990' and  datadoc = CONVERT(varchar(10),(dateadd(dd, -1, getdate())),120) ";