C# 我的销售发票的字母数字编号

C# 我的销售发票的字母数字编号,c#,sql,C#,Sql,我希望我的销售发票使用字母数字C#,并将其存储到SQL数据库中 例如,LG/slae/1000、LG/sales/1001等 这是我目前的代码。它只适用于int值。如何修改它以使用字母数字值 public class SALESINVOICE { public string Sales_Invoice {get; set;} public string Customer_Name {get; set;} public string Mobile_No {g

我希望我的销售发票使用字母数字C#,并将其存储到SQL数据库中

例如,LG/slae/1000、LG/sales/1001等

这是我目前的代码。它只适用于int值。如何修改它以使用字母数字值

     public class SALESINVOICE
{
    public string Sales_Invoice {get; set;}
    public string Customer_Name {get; set;}
     public string   Mobile_No {get; set;}
       public string Address {get; set;}
      public string  Item_Name {get; set;}
      public string  Item_Code {get; set;}
      public string  Dept {get; set;}
      public string  Date {get; set;}
       public int Qty {get; set;}
       public int Rate {get; set;}
       public int Vat_tax {get; set;}
       public int Amount {get; set;}
       public int Payment_Type {get; set;}
       public int Purchase_ID { get; set; }

       string myConnection = ("user id=username;" +
                                   "password=password;server="";" +
                                   "Trusted_Connection=yes;" +
                                   "database=""; " +
                                   "connection timeout=30");

    # region
    public void retriveData()
    {
        string selQuery="select sales_invoice from sales";
        try
        {
            SqlConnection conn= new SqlConnection (myConnection);
            conn.Open();
            SqlCommand cmd = new SqlCommand (selQuery, conn);
            Sales_Invoice=(string)cmd.ExecuteScalar()+1;
            conn.Close();
        }
        catch
        {
            Sales_Invoice=LG/SALES/1000;

您的
ExecuteScalar
可能会返回类似“LG/SALES/1000”的字符串。您正在尝试将整数值(1)添加到该值,但这将不起作用。您可能希望将字符串解析(即
Split()
方法)为3部分,将最后一部分转换为整数,然后将1添加到该整数中。然后可以将更新后的整数转换回
字符串
,使用
Join()
方法将各部分重新组合在一起(以“/”分隔),以便使用/存储新值

另外,在
catch
块中,您分配的值显然是字母数字(字符串),但该值周围没有双引号。这也行不通

至少解决这两件事,如果你还有一个问题,也许可以澄清你的问题

编辑,在OP的评论之后

好的,下面是一个简单WinForms应用程序的代码(1个表单、5个文本框和一个按钮),当您看到下面的代码时,所有这些都是不言自明的:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Increment();
    }

    private void Increment() 
    { 
        //Split the original string
        //NOTE: textBox1 already contains the text "LG/SALES/1000" in this example
        string[] tempArray = textBox1.Text.Split('/');

        //Display resulting array just for verification
        textBox2.Text = tempArray[0];
        textBox3.Text = tempArray[1];
        textBox4.Text = tempArray[2];

        //Convert last part to an int and add 1
        string tempId = tempArray[2]; 
        int id = Convert.ToInt32(tempId);
        id = id + 1; 

        //Make new alpha-numeric value with incremented id
        string autoId = "LG/SALES/" + String.Format("{0:0000000}", id); 
        textBox5.Text = autoId;
    }

}
请注意,我使用的代码与您在评论中发布的代码非常相似。因此,换句话说,它是有效的。我不确定您为什么要按您的方式设置“递增”数字的格式(您使原始的“1000”递增,并使用前导零进行格式化,因此结果变为“00001001”-您可能只需要使用“G”对于常规格式,或者只是int的
.ToString()
方法,但这是您的选择


希望这有帮助!

我不清楚您在问什么。请将代码块缩减到仅相关部分,并尝试澄清您在问什么。如果没有数据库架构,调试非常困难。您介意发布表“sales”的架构吗?这是关于编译器错误的问题吗?应该至少有2个错误。发布错误,如果这就是你的问题所在。你是在问如何在SQL中存储字母数字值吗?这只是一个
varchar
。先生,实际上我需要的是我必须以字母数字格式生成销售发票号,请帮我,先生,我刚刚尝试了拆分方法,但值没有显示我的label18这是我的代码特权e void Increment(){var str=label18.Text;var tempArray=str.Split('/');在此处输入代码字符串tempId=tempArray[2];int id=Convert.ToInt32(tempId);id=id+1;string autoId=“LG/SALES/”+string.Format(“{0:0000000}”,id);label18.Text=autoId;请参阅上面我编辑的答案,但也请利用调试器,逐步完成代码,观察变量等。如果使用调试器,则更容易查看过程中的实际情况。