如何使用c#Winforms执行字符串和整数?

Sri*_*ari 4 c# ms-access winforms

在我的发票表格中textEdit1,它"INV001"最初是在第一次显示.然后我们将Invoice表单详细信息存储到MS Access数据库.下次on-wards会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();
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*ter 5

我会存储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'));
    // ...
}
Run Code Online (Sandbox Code Playgroud)

如果你坚持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'));
Run Code Online (Sandbox Code Playgroud)

Substring如果有前导零,则无论如何都无关紧要.