增加c#数据库中的值
我试图在将每一行添加到标签后增加数据库中的值,直到第三张发票为止。这是我的密码:增加c#数据库中的值,c#,C#,我试图在将每一行添加到标签后增加数据库中的值,直到第三张发票为止。这是我的密码: public void loadInv() { >declare variables int i; int y; y = 0; i = 1; >declare data source string datasource = @"Data Source=DESKTOP-VVM3FB0\WARRENDB;Ini
public void loadInv()
{
>declare variables
int i;
int y;
y = 0;
i = 1;
>declare data source
string datasource = @"Data Source=DESKTOP-VVM3FB0\WARRENDB;Initial Catalog=mAdjustments; User ID=WarrenDB; password=//purposely taken out; ";
>declare selectquery variable
string selectQuery;
>create sql connection
SqlConnection con = new SqlConnection(datasource);
>open sql connection
con.Open();
>initialize the select query with sql query
selectQuery = @"SELECT MAX(InvoiceNum) FROM Invoices";
>initialize command with parameter of select query with connection
SqlCommand com = new SqlCommand(selectQuery, con);
> declare data reader and execute the command
SqlDataReader dr = com.ExecuteReader();
>conditional statement while reader is reading from database
while (dr.Read())
{
>if database has no row
if (dr.IsDBNull(y))
{
lblInvNum.Text = 1.ToString();
}
>if database has row
else if (dr.HasRows)
{
>>count the amount on the field and add 1
i = dr.FieldCount + 1;
i = i + 1;
>>assign to label
lblInvNum.Text = i.ToString();
}
}
}
有人能帮我弄清楚吗 您似乎正在查找最后插入的行,因此您可以在sql查询中使用输出参数和scope_变量将最后插入的行ID返回到输出参数您好,Warren,关于如何改进您的问题的一些提示:(1)选择一个描述问题本质的较短标题。将实际问题(如标题所示)移至内容的开头。(2) 在代码行之间使用普通的C#注释,而不是标记引号。它确实扩展了您的代码,使其更难阅读。(3) 再描述一下你的问题。您谈到更改值(增加),但代码仅包含select SQL语句