【问题标题】:Read .csv file, store in SQL database?读取 .csv 文件,存储在 SQL 数据库中?
【发布时间】:2013-10-28 12:59:35
【问题描述】:

对于一个编程任务,我要编写一个原型。 现在我只希望编码 1 方法。 该方法应该读取区域设置的 .csv 文件,并将其作为 BLOB(二进制大对象)保存在外部服务器上的数据库中。 但是,我对 C# 很陌生,而且我习惯了 Java。我不太确定 BLOB 是什么,是某种字节数组还是什么?

到目前为止我的程序可以

  • 连接到 SQL 服务器。
  • 读取 .csv 文件。

数据库表称为 tblUsers。

我要插入的字段是 BlobFile,它的数据类型为 varbinary(8000)。

我什至不知道数据类型是否正确。

我想要的只是将 .csv 文件保存到服务器上的表格中。

“字符串或二进制数据将被截断。语句已终止。” 不幸的是,我可以弄清楚这意味着我的 .csv 文件与表中的数据类型有些不匹配。 我不知道如何将您链接到 .csv,但它看起来像:

4.012 3.012 1.312 3.321 4.232

等等。 这是C#代码

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 System.IO;


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


    private void button1_Click(object sender, EventArgs e)
    {
        string filePath = "C:/Users/Soeren/Desktop/epilepsi - semester/EpilepsiEKG/Patient1_Reciprocal_HFpower_x1.csv";
        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        string line = fs.ReadLine();
        string[] value = line.Split(',');
        BinaryReader reader = new BinaryReader(fs);


        byte[] BlobValue = reader.ReadBytes((int)fs.Length);
        fs.Close();
        reader.Close();
        //FILE READ!

        SqlConnection con = new SqlConnection(@"Data Source=webhotel10.iha.dk;Initial Catalog=F13ST2ITS2201270867;Persist Security Info=True;User ID=F13ST2ITS2201270867;Password=F13ST2ITS2201270867");
        SqlCommand com = new SqlCommand("insert into tblUsers(BlobFilename,BlobFile) values(@BlobFilename,@Blobfile)", con);
        SqlParameter BlobFileNameParam = new SqlParameter("@BlobFileName", SqlDbType.NChar);
        SqlParameter BlobFileParam = new SqlParameter("@BlobFile", SqlDbType.Binary);
        com.Parameters.Add(BlobFileNameParam);
        com.Parameters.Add(BlobFileParam);

        BlobFileNameParam.Value = filePath.Substring(filePath.LastIndexOf("/") + 1);

        BlobFileParam.Value = BlobValue;

        try
        {

            com.Connection.Open();
            com.ExecuteNonQuery();
            MessageBox.Show(BlobFileNameParam.Value.ToString() + " saved to database.", "BLOB Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }


        catch (Exception ex)
        {

            MessageBox.Show(ex.Message, "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }


        finally
        {

            com.Connection.Close();

        }
    }

}

}

【问题讨论】:

  • 也就是说,错误消息 字符串或二进制数据将被截断。该语句已终止。 实际上说明了问题所在。您正试图将过多数据插入到具有特定维度的字段中。

标签: c# sql csv blob


【解决方案1】:

BlobFileParam 参数的声明方式,它是一个固定长度的二进制参数,大小为 0(因为没有指定大小)。您尝试插入长度大于指定大小 (0) 的数据。 尝试将参数 BlobFileParam 的 size 属性设置为正确的长度;我也认为 SqlDbType.VarBinary 可能是参数类型的更好选择。

【讨论】:

  • 这很有用。我的问题是我的 .csv 文件改变了大小,它是可变的。那么 size 属性 - 在这种情况下是什么?是最大尺寸吗?
  • 我认为将 Parameter 类型更改为 VarBinary 以及 8000 的大小(在数据库中定义)应该可以工作。然后参数类型和大小与数据库定义相匹配。
  • SqlParameter BlobFileParam = new SqlParameter("@BlobFile", SqlDbType.VarBinary, 8000);它不起作用。同样的错误。 :(
  • 我刚刚也检查了其他参数。它是 NChar 类型,也应该是 NVarChar。这也可能是错误的根源。
  • 这行得通!解决了。我想我欠你一些啤酒,如果你从丹麦的奥胡斯过来,我会打招呼的,哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2019-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多