【问题标题】:How to performing with string & integer using c# Winforms?如何使用 c# Winforms 执行字符串和整数?
【发布时间】:2014-02-10 17:13:12
【问题描述】:

在我的发票表单中,我有textEdit1,它最初显示"INV001"。然后我们将该发票表单详细信息存储到 MS Access 数据库中。下次继续 textEdit1 会自动显示下一个发票编号,例如 "INV002"。如何执行此操作?

如果只是数字,我试过这个代码成功,但现在我也有 3 个字母,那么如何执行呢?

OleDbConnection con =
    new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Srihari/Invoice.accdb");
con.Open();

OleDbCommand cmd =
    new OleDbCommand(
        "SELECT * FROM NewInvoice_1 WHERE InvoiceNumber = (select max(InvoiceNumber) from NewInvoice_1)",
        con);

OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    int a = reader.GetInt32(1);
    TXE_Invoice_Number.Text = (1 + a).ToString();
}

【问题讨论】:

  • 您将值分配给同一个文本框,它只会保留最后一个值。这就是你想要的吗?
  • 不,我需要下一个值

标签: c# winforms ms-access


【解决方案1】:

我会在数据库中存储int 而不是string

using(var con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Srihari/Invoice.accdb"))
using(var cmd = new OleDbCommand("SELECT MAX(InvoiceNumber) from NewInvoice_1", con))
{
    con.Open();
    int max = 0;
    object objMax = cmd.ExecuteScalar();
    if(objMax != null) max = (int) objMax;
    int newMax = max++; // insert this into the database instead of the string "INV001"
    // you can use newMax.ToString("000") or ToString("D3") or ToString().PadLeft(3, '0')
    string newNumber = string.Format("INV{0}", newMax.ToString().PadLeft(3, '0'));
    // ...
}

如果你坚持string和这种模式"INV001"

string maxStr = (string)cmd.ExecuteScalar() ?? "INV0";
int newMax = int.Parse(maxStr.Substring(3)) + 1;
string newNumber = string.Format("INV{0}", newMax.ToString().PadLeft(3, '0'));

在这个Substring-aproach 中是否有前导零并不重要。

【讨论】:

    【解决方案2】:

    使用此功能,您可以从文本中提取数字 - 然后添加 1 并再次添加文本。

    小心。此函数使用德国数字格式,其中逗号是小数分隔符,Point 是千位分隔符!

        public static int ConvertToIntEx(object value) {
            try {
                if (value == null) return 0;
                return Convert.ToInt32(ConvertToDecimalEx(value));
            } catch {
                return 0;
            }
        }
    
        /// <summary>
        /// Tryies to convert the string to a decimal (like VB val() )
        /// </summary>
        /// <param name="value"></param>
        /// <returns>null if the string does not start with a valid decimal number</returns>
        public static decimal? ConvertToDecimalEx(object value) {
            if (value == null || string.IsNullOrWhiteSpace(value.ToString())) return null;
            var ret = string.Empty;
            foreach (var c in value.ToString()) {
                if (c == ' ') continue;
                if (c == '.') continue;
                if (c == ',' || c == '-') {
                    if (c == ',' && ret.Contains(",")) continue;
                    if (c == '-' && ret.Contains("-")) continue;
                    ret += c.ToString();
                    continue;
                }
                if (!IsNumeric(c)) {
                    if (ret.Length > 0) {
                        //, or slash followed by non numeric chars are ignored
                        if (ret == "," || ret == "-" || ret == ",-" || ret == "-,") {
                            ret = string.Empty;
                            continue;
                        }
                        break; //Ignore Letters in front of numbers
                    }
                } else {
                    ret += c.ToString();
                }
            }
            ret = ret.Trim();
            if (ret.IndexOf("-") > 0) { //Remove all Slashes not at the beginning otherwise the number would also be interpreted as minus
                ret = ret.Replace("-", "");
            }
            if (IsNumeric(ret)) {
                return ConvertToDecimal(ret);
            } else {
                return null;
            }
        }
    
    
        /// <summary>
        /// IsNumeric Function - returns true if the Expression is a valid Number or valid decimal Number
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(object expression) {
            // Define variable to collect out parameter of the TryParse method. If the conversion fails, the out parameter is zero.
            double retNum;
    
            // The TryParse method converts a string in a specified style and culture-specific format to its double-precision floating point number equivalent.
            // The TryParse method does not generate an exception if the conversion fails. If the conversion passes, True is returned. If it does not, False is returned.
            var expressionStr = Convert.ToString(expression).Replace(".", string.Empty);
            var isNum = Double.TryParse(
                expressionStr,
                System.Globalization.NumberStyles.Any,
                System.Globalization.NumberFormatInfo.InvariantInfo,
                out retNum);
    
            return isNum;
        }
    

    【讨论】:

      猜你喜欢
      • 2023-02-06
      • 2011-05-12
      • 1970-01-01
      • 2012-04-14
      • 2011-11-10
      • 1970-01-01
      • 2015-01-27
      • 2020-02-10
      • 2016-04-19
      相关资源
      最近更新 更多