wpf image double insert c#mysql [英] wpf image double insert c# mysql

查看:69
本文介绍了wpf image double insert c#mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个项目中使用WPF和C#。

只有一个Insert按钮和一个datagrid。

首先我从datagrid中选择一个图像,

然后我按下Insert按钮。 />
在我的情况下,当我按一次插入时,两个图像同时插入我的数据库。

我的代码中的问题在哪里?:

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="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.ItemBindingGroup>
            <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" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="btnInsert" Content="Insert" HorizontalAlignment="Left" Margin="93,117,0,0" VerticalAlignment="Top" Width="75" Click="btnInsert_Click"/>
    </Grid>
</Window>







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();
        }
    }
}

推荐答案

正如我上次说的那样你发布了这个问题:< br $> b $ b

嗯...因为你告诉它了吗?

As I said last time you posted this question:

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...


这篇关于wpf image double insert c#mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆