我正在尝试从磁盘上传文件,然后将该文件插入到varbinary db列中。
我似乎无法弄清楚如何插入二进制文件。
我在WPF应用程序中使用C#和Linq to Sql。
这是我到目前为止所尝试的内容!任何建议或意见将不胜感激。
private void UploadFile()
{
DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title = "Select file";
FileData fd = new FileData();
if (dlgOpen.ShowDialog() ?? false)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
//FileStream outStream = File.OpenWrite(dlgOpen.FileName + ".xlsx");
int b;
while ((b = inStream.ReadByte()) > -1)
// outStream.WriteByte((byte)b);
fd.FileId = Guid.NewGuid();
//fd.DataFile = inStream;//DataFile is the Varbinary column in the db
fd.Title = dlgOpen.FileName;
fd.FileExtension = txtExtension.text;
context.FileDatas.InsertOnSubmit(fd);
context.SubmitChanges();
//outStream.Flush();
//outStream.Close();
inStream.Close();
}
}
}
答案 0 :(得分:1)
要修复编译错误,请删除while
语句。您正在尝试创建一个新的FileData(),在b > -1
之前永远不能使用它。
我不知道代码会在那之后工作,但它会解决这个编译错误。
private void UploadFile()
{
DatabaseData.DataClassesDataContext context = new DatabaseData.DataClassesDataContext();
{
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Title = "Select file";
if (dlgOpen.ShowDialog() ?? false)
{
FileStream inStream = File.OpenRead(dlgOpen.FileName);
FileData fd = new FileData();
fd.FileId = Guid.NewGuid();
fd.DataFile = inStream;
fd.Title = dlgOpen.FileName;
fd.FileExtension = txtExtension.text;
context.FileDatas.InsertOnSubmit(fd);
context.SubmitChanges();
inStream.Close();
}
}
}
答案 1 :(得分:1)
不确定这是否有效,但请尝试此
if (dlgOpen.ShowDialog() ?? false)
{
byte[] bytes = System.IO.File.ReadAllBytes(dlgOpen.FileName);
fd.FileId = Guid.NewGuid();
fd.DataFile = bytes;
fd.Title = dlgOpen.FileName;
context.FileDatas.InsertOnSubmit(fd);
context.SubmitChanges();
答案 2 :(得分:1)
嗯,您在评论中阅读了我的免责声明。我不能保证这里的任何专业人员都会同意根据您的要求采用的方法。我正在以正确的方式学习C#,并且有了转换一个有效的非数据库程序的想法。我需要将所有现有数据转换为一个新的数据库来接管存储:
/* Spawned from a button click
...
*/
//
// Here I bring in the directory which you'll likely replace with
// a single file
//
string[] files =
Directory.GetFiles(
@"yourDicectory");
//
// At this point you may disregard my loop if needed
//
foreach (string file in files)
{
//
// Here the entire files are read and split
// Handle your data how you like
//
StreamReader fileReader = new StreamReader( file );
string lines = fileReader.ReadToEnd();
string[] entries = lines.Split( ',' );
//
// Here, omitted, I declare variables of types to insert "holders"
// Every CSV has to go to a corresponding holder of the
// the appropriate type (i.e., DateTime, decimal(money), or yourType)
//
SqlCeConnection con = new SqlCeConnection( "Data Source = YourDataSource.sdf" );
con.Open();
SqlCeCommand cmd = con.CreateCommand();
//
// The insert command that takes the parsed values - value1, value2, ...
// which are the named and omitted declarations from above
// You're providing a signature of the table you're inserting into
//
cmd.CommandText = "INSERT INTO YourTable ([Column1], [Column2], [Column3], ... , [Column(n)]) VALUES (value1, value2, value3, ... , value(n))";
//
// Here, omitted, I parse and convert the values and store them in the holders
//
// Now execute and catch if needed
try
{
cmd.ExecuteNonQuery();
}
catch( SqlCeException sqle )
{
myTextbox.Text += sqle.Errors.ToString() + "\n";
}
}
//
// Update my view - May not apply
//
myGridView1.Update();
con.Close();
}
/* Do whatever else you'd like ... */
答案 3 :(得分:0)