C# 使用c从MySQL插入和检索音频文件
我成功地在MySQL数据库中保存了一个音频文件,但我不知道如何从数据库中检索和播放它。任何帮助都将不胜感激 这是我的插入代码:C# 使用c从MySQL插入和检索音频文件,c#,mysql,C#,Mysql,我成功地在MySQL数据库中保存了一个音频文件,但我不知道如何从数据库中检索和播放它。任何帮助都将不胜感激 这是我的插入代码: private void button2_Click(object sender, EventArgs e) { try { MySqlConnection connection; string cs = @"server = 127.0.0.1; userid = root; pwd = ; database = at
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);
}
}
}
}
如果您还没有制定出解决方案,我只是根据此链接中的答案制定了解决方案: 在这个链接的末尾,它给出了Dewplayer zip包的位置。下载并将dewplayer-vol.swf文件放入根文件夹 您需要将所有MS SQL命令转换为MySQL,并将MySQL.DLL放入bin文件夹(如果没有)。在我的例子中,我将mp3文件存储为varbinay,并使用如下存储过程检索它:
private void BindGrid()
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlServer"].ConnectionString;
using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connStr))
{
using (MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand("getaudio", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (IDataReader idr = cmd.ExecuteReader())
{
GridView1.DataSource = idr;
GridView1.DataBind();
}
conn.Close();
}
}
}
有关此链接的更多详细信息:
您需要创建一个文件,比如Audio.ashx,并将其放入根文件夹。希望这能帮助你 我不明白。您已将这些值保存到数据库中,但无法从数据库中读取它们?你应该经常使用。这种类型的字符串连接对攻击是开放的。并使用using语句处理数据库连接和对象我可以检索其余的数据,但不能检索音频文件。我尝试使用media player播放音频文件,但它说这些文件具有.datarowview扩展名