對於編程排序,我將編寫一個原型。 現在我只想找代碼1的方法。 該方法應該讀取一個語言環境.csv文件,並將其另存爲BLOB(二進制大對象)在外部服務器上的數據庫中。但是,我在C#上很新,而且我習慣於Java。我不完全確定BLOB是什麼,某種字節數組或什麼?讀取.csv文件,存儲在SQL數據庫中?
到目前爲止,我的程序可以
- 連接到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();
}
}
}
}
這就是說,錯誤消息**字符串或二進制數據將被截斷。該聲明已被終止。**實際上闡明瞭什麼是錯誤的。您正嘗試將太多數據插入到具有特定維度的字段中。 – Izikon