-2
我有一些任务。我必须实现一个文本编辑器,可以将文件保存到/从ms访问数据库中下载,但我不知道该怎么做。 有我的尝试(它不工作,只保存文件)。我知道,这是愚蠢的,但..如何将* .dat(或* .txt)文件插入到C上的MS Access数据库中#
OleDbCommand cmd = new OleDbCommand();
OleDbConnection cn = new OleDbConnection(
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\..\Database1.accdb;Persist Security Info=True");
private void Button_Click_2(object sender, RoutedEventArgs e)
{
if(textBox1.Text != null)
{
FileInfo file = new FileInfo(textBox1.Text.ToString() + ".dat");
using (BinaryWriter bw = new BinaryWriter(file.OpenWrite()))
{
string text = textBox2.Text.ToString();
bw.Write(text);
try
{
cn.Open();
cmd.CommandText = "INSERT INTO info (files.FileData) values (@file)";
cmd.Parameters.Add("@file", file);
cmd.ExecuteNonQuery();
cn.Close();
}
catch (Exception ex)
{
cn.Close();
MessageBox.Show(ex.Message.ToString());
}
textBox1.Text = null;
textBox2.Text = null;
}
}
}
有没有什么办法可以直接上传文件到数据库?
你能更清楚哪里出了问题,你的问题是什么? – BradleyDotNET 2015-01-20 23:57:24
我无法将文件插入数据库。我的代码是错误的。我认为这行不正确cmd.Parameters.Add(“@ file”,file); – 2015-01-21 00:16:09
那么你想要放入数据库?所有的文字?二进制?你不能只是“把一个文件”放在数据库中 – BradleyDotNET 2015-01-21 00:21:33