C#如何更新/更改数据库中字符串的特定字母/数字

时间:2018-10-04 15:06:22

标签: c# winforms

我问这些问题,因为我真的不知道该怎么办,有可能做到吗?
我想要的是在此处更新/更改例如数据库值中的STA-100418-100,根据用户输入更新/更改100,例如50就是STA-100418-50。

这里提供的图片更加精确 enter image description here 如您在图像上看到的,有一条红线,如果用户将数量更新为60,则在Codeitem STA-100418-100中应为STA-100418-60 我真的不知道该怎么做。我希望有人能够帮助我

这是我更新数量的代码

private void btn_ok_Click(object sender, EventArgs e)
{
    using (var con = SQLConnection.GetConnection())
    {
        using (var selects = new SqlCommand("Update Product_Details set quantity = quantity - @Quantity where ProductID= @ProductID", con))
        {
            selects.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = _view.txt_productid.Text;
            selects.Parameters.Add("@Quantity", SqlDbType.Int).Value = Quantity;
            selects.ExecuteNonQuery();                          
        }
    }
}

这是在代码项中获取该格式的代码

string date = DateTime.Now.ToString("MMMM-dd-yyyy");
string shortdate = DateTime.Now.ToString("-MMddy-");
private void Quantity_TextChanged(object sender, EventArgs e)
    {
        Code.Text = Supplier.Text.Substring(0, 3) + shortdate + Quantity.Text;
    }

4 个答案:

答案 0 :(得分:0)

用户每次都在“数量”文本框中输入内容,更改数量的事件将被命中,并且您每次都会获得相同日期但数量不同的新值。因此,您可以使用Code.Text更新CodeDateTime值,也可以使用全局变量代替Code.Text并使用它来更新列。

string date = DateTime.Now.ToString("MMMM-dd-yyyy");
string shortdate = DateTime.Now.ToString("-MMddy-");
private void Quantity_TextChanged(object sender, EventArgs e)
{
    Code.Text = Supplier.Text.Substring(0, 3) + shortdate + Quantity.Text;
}

        using (var con = SQLConnection.GetConnection())
        {
            using (var selects = new SqlCommand("Update Product_Details set quantity = quantity - @Quantity, CodeItem = @Code where ProductID= @ProductID", con))
            {
                selects.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = _view.txt_productid.Text;
                selects.Parameters.Add("@Quantity", SqlDbType.Int).Value = Quantity;
                selects.Parameters.Add("@Code", Code.Text);
            }
        }

答案 1 :(得分:0)

这就是我用来更新SQL Server的内容

public static DataTable GetSqlTable(string sqlSelect)
    {
        string conStr = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
        DataTable table = new DataTable();
        SqlConnection connection = new SqlConnection(conStr);
        try
        {
            connection.Open();
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.ToString());
        }

        if (connection.State != ConnectionState.Open)
        {
            return table;
        }

        SqlCommand cmd = new SqlCommand(sqlSelect, connection);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);

        try
        {
            adapter.Fill(table);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
            throw;
        }
        connection.Close();
        connection.Dispose();

        return table;
    }

public static void GetSqlNonQuery(string sqlSelect)
    {
        string newObject = string.Empty;
        string strConn = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
        SqlConnection connection = new SqlConnection(strConn);
        connection.Open();

        if (connection.State != ConnectionState.Open)
        {
            return;
        }

        try
        {
            SqlCommand cmd = new SqlCommand(sqlSelect, connection);
            cmd.ExecuteNonQuery();
            connection.Close();
            connection.Dispose();
        }
        catch (Exception ex)
        {
            string x = ex.Message + ex.StackTrace;
            throw;
        }

    }

这里是使用方法

DataTable dt = GetSqlTable("select Quantity from product where CodeItem = 'STA-100418-100'");
string strQuantity = dt.Rows[0]["Quantity"].ToString();
GetSqlNonQuery(string.Format("UPDATE product SET CodeItem = '{0}' WHERE  = 'STA-100418-100'", strQuantity));

答案 2 :(得分:0)

据我了解,您需要MSSQL字符串函数

SELECT rtrim(left(Codeitem,charindex('-', Codeitem))) + ltrim(str(Quantity)) FROM ...

For detailed information

答案 3 :(得分:0)

使用MySql,对代码项进行子字符串化,然后将数量串联起来。

UPDATE Product_Details SET quantity = @quantity,CodeIem = CONCAT(SUBSTR(@code,1,11),@quantity) WHERE ProductID= @ProductID