我正在使用c#中的应用程序从数据库中获取数据。该应用程序有一些字段,如:条形码,产品,数量,价格,总数
我使用此代码从数据库中获取数据:
if (e.Key == Key.Enter)
{
SqlConnection con = new SqlConnection("Server = localhost;Database = Bilanc; Integrated Security = true");
SqlCommand cmd = new SqlCommand("Product", con); // Using a Store Procedure.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("Barcode", txtcode.Text);
dtg.ItemsSource = dataTable.DefaultView;//Set the DataGrid ItemSource to this new generated DataTable
con.Open();//Open the SQL connection
SqlDataReader reader = cmd.ExecuteReader();//Create a SqlDataReader
while (reader.Read())//For each row that the SQL query returns do
{
DataRow dr = dataTable.NewRow();//Create new DataRow to populate the DataTable (which is currently binded to the DataGrid)
dr[0] = reader[0];//Fill DataTable column 0 current row (Product) with reader[0] (Product from sql)
dr[1] = reader[1];
dr[2] = reader[2];
dr[3] = reader[3];
dr[4] = reader[4];
dataTable.Rows.Add(dr);//Add the new created DataRow to the DataTable
txtcode.Text = "";
object sumObject;
sumObject = dataTable.Compute("Sum(Total)", "");
txttot.Text = sumObject.ToString();
}
}
}
xaml代码
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
<RowDefinition Height="100"/>
<RowDefinition Height="50"/>
<RowDefinition Height="700"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBox Name="txtb"/>
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<Label Name="lblkodi" Content="Kodi"/>
<Label Name="lblpershkrimi" Margin="900,0,0,0" Content="Pershkrimi" Visibility="Hidden"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<TextBox Name="txtkodi" Width="400" Height="30" KeyDown="kerko" />
<TextBox Name="txtpershkrimi" Margin="480,0,0,0" Width="400" Height="30" Visibility="Hidden"/>
</StackPanel>
<StackPanel Grid.Row="3" HorizontalAlignment="Right">
<TextBox Name="txttot" Width="350" Height="80" BorderBrush="Black" Text="0" FontSize="50" TextAlignment="Center" Foreground="DarkBlue"/>
</StackPanel>
<StackPanel Grid.Row="5" Orientation="Horizontal">
<DataGrid Name="dtg" CurrentCellChanged="data" Height="650"
HorizontalAlignment="Left" Margin="12,21,0,0"
VerticalAlignment="Top" RowHeight="30" ColumnWidth="150"
RowBackground="LightYellow"
AlternatingRowBackground="LightBlue"
AutoGenerateColumns="True" BorderBrush="Black" BorderThickness="4"
CanUserResizeColumns="True" Foreground="Black" Width="1250" />
</StackPanel>
<StackPanel Grid.Row="6" Orientation="Horizontal">
<Button Name="btnfaturo" Width="170" Content="Faturo" Height="60"
Click="btnfaturo_Click" Margin="40,0,0,0" />
<Button Name="btnfatura" Width="170" Content="Fatura e rregullt"
Height="60" Click="btnfatura_Click" />
<Button Name="btnanulo" Width="170" Content="Hiq Artikuj(F7)"
Height="60" Click="btnanulo_Click" />
<Button Name="btnzbritje" Width="170" Content="Zbritje ne total" Height="60" Click="btnzbritje_Click" />
<Button Name="btncmimi" Width="170" Content="Ndrysho Cmimin" Height="60" Click="btncmimi_Click" />
<Button Name="btnpershkrim" Width="170" Content="Sipas pershkrimit" Height="60" Click="btnpershkrim_Click" KeyDown="F1" />
<Button Name="btnlc" Width="170" Content="LC" Height="60" />
</StackPanel>
存储过程
ALTER procedure [dbo].[product]
@Barcode varchar (50) = null,
@Product varchar (50) = null,
@price int = null,
@qty int = null,
@tax varchar (50) = null
AS
SELECT[Barcode]
,[Product]
,[Price]
,[qty]
,[tax]
FROM [Bilanc].[dbo].[artiku]
Where Barcode LIKE @Barcode
代码工作正常,但我遇到了另一个问题。例如,用户将数据网格中的数量从1更改为3我希望在用户点击按钮后还可以扣除3个项目(待更新)中的数据库中的数量 如果有人能帮助我,因为我是编程的新手,这是我必须完成的功课 感谢任何人。