双图像插入 [英] double image insert
问题描述
我在这个项目中使用WPF和C#。
只有一个Insert按钮和一个datagrid。
首先我从datagrid中选择一个图像,
然后我按下Insert按钮。 />
在我的情况下,当我按一次插入时,两个图像同时插入我的数据库。
我的代码中的问题在哪里?:
< window x:class =PupilReg.MainWindowxmlns:x =#unknown>
xmlns =http://schemas.microsoft .com / winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title = 图像插入高度=674宽度=1009WindowStartupLocation =CenterScreenFontSize =15FontWeight =BoldResizeMode =CanResizeWithGrip>
< grid margin = 0,10,-8,-50>
< datagrid name =dataGrid1height =565width =435margin =474,53,100,65>
< datagrid.itembindinggroup>
< bindinggroup>
< datagrid.columns>
< datagridtextcolumn header =Image_IDbinding ={Binding Path = Image_ID}width =80>
< datagridtemplatecolumn header =Image_BLOBwidth =200isreadonly =True>
< datagridtemplatecolumn.celltemplate>
< datatemplate>
< Image Source ={Binding Path = Image_BLOB}Width =160Height =160/>
<按钮x:名称=btnInsertContent =InsertHorizontalAlignment =LeftMargin =93,117, 0,0VerticalAlignment =TopWidth =75Click =btnInsert_Click/>
使用MySql.Data.MySqlClient;
使用System;
使用System.Data;
使用System.Windows;
命名空间PupilReg
{
公共部分类MainWindow:Window
{
string constr =Data Source = localhost; port = 3306; Initial Catalog = test; User Id = root; password = 2525;
public MainWindow()
{
InitializeComponent();
BindGrid1();
}
public void BindGrid1()
{
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand( SELECT * FROM images,con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
private void btnInsert_Click(object sender,RoutedEventArgs e )
{
DataRowView SelectedRowValue =(DataRowView)dataGrid1.SelectedValue;
byte [] ImageBytes =(byte [])SelectedRowValue.Row.ItemArray [1];
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand(INSERT INTO images(Image_BLOB)VALUES(@ImageSource),con);
cmd.Parameters.Add(@ ImageSource,MySqlDbType.Blob,ImageBytes.Length).Value = ImageBytes;
con.Open();
int i = cmd.ExecuteNonQuery();
if(i> 0)
{
cmd.ExecuteNonQuery();
MessageBox.Show(Image Inserted);
}
con.Close();
BindGrid1();
}
}
}
I use WPF and C# in this project.
There is only an Insert button and a datagrid.
First I choose an image from datagrid,
then I press Insert button.
In my case when I press insert once, two images are inserted to my DB at the same time.
Where is the problem in my code?:
<window x:class="PupilReg.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Image Insert" Height="674" Width="1009" WindowStartupLocation="CenterScreen" FontSize="15" FontWeight="Bold" ResizeMode="CanResizeWithGrip">
<grid margin="0,10,-8,-50">
<datagrid name="dataGrid1" height="565" width="435" margin="474,53,100,65">
<datagrid.itembindinggroup>
<bindinggroup>
<datagrid.columns>
<datagridtextcolumn header="Image_ID" binding="{Binding Path=Image_ID}" width="80">
<datagridtemplatecolumn header="Image_BLOB" width="200" isreadonly="True">
<datagridtemplatecolumn.celltemplate>
<datatemplate>
<Image Source="{Binding Path=Image_BLOB}" Width="160" Height="160" />
<Button x:Name="btnInsert" Content="Insert" HorizontalAlignment="Left" Margin="93,117,0,0" VerticalAlignment="Top" Width="75" Click="btnInsert_Click"/>
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows;
namespace PupilReg
{
public partial class MainWindow : Window
{
string constr = "Data Source=localhost;port=3306;Initial Catalog=test;User Id=root;password=2525";
public MainWindow()
{
InitializeComponent();
BindGrid1();
}
public void BindGrid1()
{
MySqlConnection con = new MySqlConnection(constr);
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM images", con);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dataGrid1.ItemsSource = dt.DefaultView;
}
private void btnInsert_Click(object sender, RoutedEventArgs e)
{
DataRowView SelectedRowValue = (DataRowView)dataGrid1.SelectedValue;
byte[] ImageBytes = (byte[])SelectedRowValue.Row.ItemArray[1];
MySqlConnection con = new MySqlConnection(constr);
MySqlCommand cmd = new MySqlCommand("INSERT INTO images (Image_BLOB) VALUES (@ImageSource)", con);
cmd.Parameters.Add("@ImageSource", MySqlDbType.Blob, ImageBytes.Length).Value = ImageBytes;
con.Open();
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
cmd.ExecuteNonQuery();
MessageBox.Show("Image Inserted");
}
con.Close();
BindGrid1();
}
}
}
推荐答案
嗯......因为你告诉它了吗?
Um...because you tell it to?
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
cmd.ExecuteNonQuery();
MessageBox.Show("Image Inserted");
}
对ExecuteNonQuery的两次调用将提供两个INSERT ...
Two calls to ExecuteNonQuery will give two INSERTs...
这篇关于双图像插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!