【问题标题】:How to summation number string form SQL Server in Label如何在标签中汇总来自 SQL Server 的数字字符串
【发布时间】:2023-04-07 23:34:01
【问题描述】:

我在 SQL Server 中获取数字并显示在带有数字的标签中:

1.1.2

我希望当按钮被按下时标签是+1。

Expectation result: 1.1.3

我在项目中使用的波纹管代码:

private void FrmMain_Load(object sender, EventArgs e)
        {
           Checkversion();
        }

public void Checkversion()
        {
            string maincon = ConfigurationManager.ConnectionStrings["Connstring"].ConnectionString;
            SqlConnection sqlconn = new SqlConnection(maincon);
            string sqlquery = ("select * from tbl_Version ");
            SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
            SqlDataAdapter da = new SqlDataAdapter();
            {
                sqlconn.Open();
                da.SelectCommand = sqlcomm;
                DataSet ds = new DataSet();
                da.Fill(ds);
                serverVersion = ds.Tables[0].Rows[0]["Version"].ToString();
                lbVerionUpdate.Text = serverVersion;
                sqlconn.Close();
            }
        }
 private void Button1_Click(object sender, EventArgs e)
        {
            int i = Convert.ToInt32(lbVerionUpdate.Text);
            i++;
            var version = i.ToString();
            lbVerionUpdate.Text = version;
        }

我知道,如果是整数就可以,但是结果是十进制,所以程序会报错: System.FormatException: '输入字符串的格式不正确。'

我尝试将 int 替换为 float 但结果相同,请帮我修复它或提供替代解决方案。谢谢

【问题讨论】:

  • 问题已解决,感谢@jarlh 的支持
  • 你的代码有一堆问题。您需要所有 SQL 对象上的 using 块。不要select *,只指定您需要的确切列。你不需要DataSetDataAdapter, just use DataTable.Load. If you only want one column, one row, then use ExecuteScalar`

标签: c# sql sql-server winforms button


【解决方案1】:

文本“1.1.2”不是数字(不是整数,也不是小数)三分之二!

您可以拆分文本,将数组的最后一项转换为整数,将其递增,然后连接数组。

喜欢

private void Button1_Click(object sender, EventArgs e)
    {
        lbVerionUpdate.Text = IncremVersion(lbVerionUpdate.Text);
    }

    private string IncremVersion(string version)
    {
        if (!string.IsNullOrWhiteSpace(version))
        {
            var nfos = version.Split('.');
            if (nfos != null && nfos.Length > 0)
            {
                string revisionTxt = nfos[nfos.Length - 1];
                if (int.TryParse(revisionTxt, out int revision))
                {
                    revisionTxt = (revision++).ToString();
                }

                nfos[nfos.Length - 1] = revisionTxt;
                return string.Join('.', nfos);
            }
        }

        // Fail to increment Version here !
        return version
    }

【讨论】:

  • return string.Join('.', nfos);无法从 'char' 转换为 'string'
  • 对不起:替换 string.Join('.', nfos); BY string.Join(".", nfos);会更好
【解决方案2】:

感谢@jarlh,问题现已解决。

我使用以下代码。

private void Button1_Click(object sender, EventArgs e)
{
    string _versionDB = lbVerionUpdate.Text;
    var version = new Version(_versionDB);
    var newRevisionVersion = new Version(version.Major, version.Minor, version.Build + 1);
    _versionDB = newRevisionVersion.ToString();
    lbVerionUpdate.Text = _versionDB;
}

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多