C# 使用Windows应用程序c将文本文件上载到mysql#

C# 使用Windows应用程序c将文本文件上载到mysql#,c#,mysql,.net,windows,visual-studio,C#,Mysql,.net,Windows,Visual Studio,我正在尝试使用C#windows应用程序(visual studio2019)将文本文件上载到mysql数据库。 但在将文件上传到数据库时遇到了一些问题 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thre

我正在尝试使用C#windows应用程序(visual studio2019)将文本文件上载到mysql数据库。 但在将文件上传到数据库时遇到了一些问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.IO;



namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        MySqlConnection connection;


        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //To where your opendialog box get starting location. My initial directory location is desktop.
            openFileDialog1.InitialDirectory = "C://Desktop";
            //Your opendialog box title name.
            openFileDialog1.Title = "Select file to be upload.";
            //which type file format you want to upload in database. just add them.
            openFileDialog1.Filter = "Select Valid Document(*.txt;*.pdf; *.doc; *.xlsx; *.html)|*.txt;*.pdf; *.docx; *.xlsx; *.html";
            //FilterIndex property represents the index of the filter currently selected in the file dialog box.
            openFileDialog1.FilterIndex = 1;
            try
            {
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    if (openFileDialog1.CheckFileExists)
                    {
                        string path = System.IO.Path.GetFullPath(openFileDialog1.FileName);
                        label1.Text = path;
                    }
                }
                else
                {
                    MessageBox.Show("Please Upload document.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string filename = System.IO.Path.GetFileName(openFileDialog1.FileName);
                if (filename == null)
                {
                    MessageBox.Show("Please select a valid document.");
                }
                else
                {
                    //we already define our connection globaly. We are just calling the object of connection.
                    MySqlConnection connection = new MySqlConnection("datasource=localhost;port=3306;username=root;password=");
                    //MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM fileuploadwindows.fileupload", connection);
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand("insert into fileupload values('\\Document\\" + filename + "')", connection);
                    string path = Application.StartupPath.Substring(0, (Application.StartupPath.Length - 10));
                    System.IO.File.Copy(openFileDialog1.FileName, path + "\\Document\\" + filename);
                    cmd.ExecuteNonQuery();
                    connection.Open();
                    MessageBox.Show("Document uploaded.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
我没有把任何东西输入数据库。请解决这个错误。数据没有进入数据库。 它显示文本文件已存在或未选择任何数据库,或列计数与第1行的值计数不匹配

帮助解决问题

保存到数据库后,我想在gridview中查看数据库中的内容

试试这个:

public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
试试这个:

public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
试试这个:

public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
解决方案1: 手动按单元格和行添加,但也需要大量的工作

解决方案2: 将结果封装到强类型对象中,如:

public class FileTable {
    public string FileName {get;set;}
        ................ etc...
}
检索后,将其放入集合中。将BindingSource对象分配给datagridview。 然后在BindingSoure.DataSource上,指定收集的包含文件信息列表数据的集合

例如:

List<FileTable> list = new List<FileTable>();
.....Assuming your list has been populated with the data from the mysql database table
BindingSource1.DataSource = list;
List List=新列表();
..…假设您的列表已填充mysql数据库表中的数据
BindingSource1.DataSource=列表;
学习如何引入一些OOP模式,使您的编码更加容易。

尝试以下方法:

public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
public void SaveToMysql() {

            byte[] rawData = File.ReadAllBytes("your file location");
            FileInfo info = new FileInfo("your file location");


            using (MySqlConnection connection = new MySqlConnection("server=localhost;uid=root;pwd=P@ssw0rd;database=yourDb;"))
            {
                using (MySqlCommand command = new MySqlCommand())
                {
                    command.Connection = connection;
                    command.CommandText = "INSERT INTO file (fileName, fileBlob) VALUES (?fileName, ?fileName);";                    
                    MySqlParameter blobName = new MySqlParameter("?fileName", MySqlDbType.String);
                    MySqlParameter blobData  = new MySqlParameter("?rawData", MySqlDbType.Blob, rawData.Length);                    

                    blobData.Value = rawData;
                    blobName.Value = info.Name;

                    command.Parameters.Add(blobData);
                    command.Parameters.Add(blobName);

                    connection.Open();

                    command.ExecuteNonQuery();

                }
            }
        }
解决方案1: 手动按单元格和行添加,但也需要大量的工作

解决方案2: 将结果封装到强类型对象中,如:

public class FileTable {
    public string FileName {get;set;}
        ................ etc...
}
检索后,将其放入集合中。将BindingSource对象分配给datagridview。 然后在BindingSoure.DataSource上,指定收集的包含文件信息列表数据的集合

例如:

List<FileTable> list = new List<FileTable>();
.....Assuming your list has been populated with the data from the mysql database table
BindingSource1.DataSource = list;
List List=新列表();
..…假设您的列表已填充mysql数据库表中的数据
BindingSource1.DataSource=列表;

学习引入一些OOP模式,这样您的编码就容易多了。

是否希望将其作为blob(即保存到表列的实际文件)添加到表中?实际上一切都很好,但现在我尝试使用varbinary将其添加到表中。因为我还需要从数据库中检索它@MaicoDo是否希望将其作为blob添加到表中(意味着实际文件保存到表的列中)?实际上一切都很好,但现在我尝试使用varbinary将其添加到表中。因为我还需要从数据库中检索它@Maicocan我还可以从数据库中检索它,并在gridview中查看它???是的,您可以创建一个查询,然后获取结果并将其转换回流,问题是,您想在datagridview列中将其显示为文件吗?或者您想在datagridview列中显示内容?我想在datagridview中将其显示为文件。。。就像datagrid中的“id1 test.txt”。谢谢。我也可以从数据库中检索它并在gridview中查看吗???是的,您可以创建一个查询,然后获取结果并将其转换回流,问题是,是否要在datagridview列中将其显示为文件?或者您想在datagridview列中显示内容?我想在datagridview中将其显示为文件。。。就像datagrid中的“id1 test.txt”。谢谢