如何使用string&;使用c#Winforms的整数?

如何使用string&;使用c#Winforms的整数?,c#,winforms,ms-access,C#,Winforms,Ms Access,在我的发票表单中,我有textededit1,它第一次显示“INV001”。然后,我们将发票表单详细信息存储到MS Access数据库中。下次在wards上textEdit1自动显示下一个发票号,如“INV002”。如何执行此操作 OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/Srihari/Srihari/Invoice.accdb"); con.O

在我的发票表单中,我有
textededit1
,它第一次显示
“INV001”
。然后,我们将发票表单详细信息存储到MS Access数据库中。下次在wards上
textEdit1
自动显示下一个发票号,如
“INV002”
。如何执行此操作

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();
}
若这是唯一的数字,我尝试了这个代码工作成功,但现在我也有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();
}

我将在数据库中存储
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'));
    // ...
}
如果您坚持使用
字符串和此模式
“INV001”


在此
子字符串中,是否有前导零无关紧要。

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

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

    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;
    }
public static int ConvertToIntEx(对象值){
试一试{
if(value==null)返回0;
返回Convert.ToInt32(ConvertToDecimalEx(value));
}抓住{
返回0;
}
}
/// 
///尝试将字符串转换为十进制(如VB val())
/// 
/// 
///如果字符串不是以有效的十进制数开头,则为null
公共静态十进制?ConvertToDecimalEx(对象值){
if(value==null | | string.IsNullOrWhiteSpace(value.ToString())返回null;
var ret=string.Empty;
foreach(value.ToString()中的变量c){
如果(c='')继续;
如果(c='.')继续;
如果(c==','| | c=='-'){
如果(c==','&&ret.Contains(“,”)继续;
如果(c='-'&&ret.Contains(“-”)继续;
ret+=c.ToString();
继续;
}
如果(!IsNumeric(c)){
如果(返回长度>0){
//,或斜杠后跟非数字字符将被忽略
如果(ret==”,“|ret==”-“|ret==”,-“|ret==”,-”){
ret=string.Empty;
继续;
}
break;//忽略数字前面的字母
}
}否则{
ret+=c.ToString();
}
}
ret=ret.Trim();
如果(ret.IndexOf(“-”)>0){//删除所有不在开头的斜杠,否则该数字也将被解释为负数
ret=ret.Replace(“-”,”);
}
如果(是数字(ret)){
返回到指定值(ret);
}否则{
返回null;
}
}
/// 
///IsNumeric函数-如果表达式是有效数字或有效十进制数,则返回true
/// 
/// 
/// 
公共静态bool IsNumeric(对象表达式){
//定义变量以收集TryParse方法的out参数。如果转换失败,out参数为零。
双网;
//TryParse方法将指定样式和区域性特定格式的字符串转换为其等效的双精度浮点数。
//如果转换失败,TryParse方法不会生成异常。如果转换通过,则返回True。如果转换不通过,则返回False。
var expressionStr=Convert.ToString(表达式).Replace(“.”,string.Empty);
var isNum=Double.TryParse(
表达,
System.Globalization.NumberStyles.Any,
System.Globalization.NumberFormatInfo.InvariantInfo,
out retNum);
返回isNum;
}

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