我将音频文件成功保存在MySQL数据库中,但我不知道如何从数据库中检索和播放。任何帮助将不胜感激。使用c插入和检索MySQL中的音频文件#
这是我插入代码:
private void button2_Click(object sender, EventArgs e)
{
try
{
MySqlConnection connection;
string cs = @"server = 127.0.0.1; userid = root; pwd = ; database = atlfmdb;";
connection = new MySqlConnection(cs);
if (upload.Text.Length > 0 &&
vName.Text.Length > 0 &&
vTel.Text.Length>0 &&
tbposition.Text.Length>0)
{
string FileName = upload.Text;
byte[] f = File.ReadAllBytes(upload.Text) ;
MySqlCommand selectcom = new MySqlCommand("insert into interinterview(intName,intPosition,intTel,audioFile)values('" + vName.Text + "','" + tbposition.Text + "','" + vTel.Text + "','" + f + "')", connection);
MySqlDataReader myread;
connection.Open();
myread = selectcom.ExecuteReader();
while (myread.Read())
{
}
connection.Close();
MessageBox.Show("Data saved successfully");
vName.Text = "";
vTel.Text = "";
tbposition.Text = "";
upload.Text = "";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Audio files | *.mp3";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
upload.Text = openFileDialog1.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
我不明白。你将这些值保存到你的数据库,但你不能从数据库中读取它们?你应该总是使用[参数化查询](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。这种字符串连接对于[SQL注入](http://en.wikipedia.org/wiki/SQL_injection)攻击是开放的。并使用'using'语句处理您的数据库连接和对象 – 2014-10-27 13:11:24
我可以检索其余数据,但不能检索音频文件。 – 2014-10-27 14:03:22
我试图使用媒体播放器播放音频文件,但它说文件有.datarowview扩展名 – 2014-10-29 13:04:55