C# 如何在单击按钮时播放数据库中的音频

C# 如何在单击按钮时播放数据库中的音频,c#,asp.net,.net,visual-studio,C#,Asp.net,.net,Visual Studio,我想在文本框中输入字母的基础上播放音频。 因此,当文本写入Textbox1时,它应该使用该特定单词获取数据库。因此,在按钮上单击Literal1 get called,它将播放音频 protected void speakBtn_Click(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (M

我想在文本框中输入字母的基础上播放音频。 因此,当文本写入Textbox1时,它应该使用该特定单词获取数据库。因此,在按钮上单击Literal1 get called,它将播放音频

protected void speakBtn_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand("select * from tblfiles5 where voicename='" + TextBox1.Text + "'",con))
        {
            MySqlDataReader mdr;

            cmd.Connection = con;
            con.Open();

            mdr = cmd.ExecuteReader();
            if (mdr.Read())
            {
                MessageBox.Show(TextBox1.Text);
                string link = "<audio controls> <Source src= "+ mdr.Read() + " type =audio/wav ></audio>";
                Literal1.Text = link;
            }
            else
            {
                MessageBox.Show("No "+ TextBox1.Text+" exist in database");
            }
                con.Close();
        }
    }
    Response.Redirect(Request.Url.AbsoluteUri);
}
protectedvoid speakBtn\u单击(对象发送者,事件参数e)
{
string constr=ConfigurationManager.ConnectionString[“constr”].ConnectionString;
使用(MySqlConnection con=newmysqlconnection(cont))
{
使用(MySqlCommand cmd=new MySqlCommand(“从tblfiles5中选择*其中voicename='”+TextBox1.Text+'”,con))
{
MySqlDataReader-mdr;
cmd.Connection=con;
con.Open();
mdr=cmd.ExecuteReader();
if(mdr.Read())
{
MessageBox.Show(TextBox1.Text);
字符串链接=”;
Literal1.Text=链接;
}
其他的
{
MessageBox.Show(“否”+TextBox1.Text+“数据库中存在”);
}
con.Close();
}
}
重定向(Request.Url.AbsoluteUri);
}
下面是我的o/p屏幕截图


您需要查看从SQL表返回的列:

从tblfiles5中选择*

然后您可以使用mdr[“Column1”].ToString(),例如:

string link = "<audio controls> <Source src= "+ mdr["Column1"].ToString() + " type =audio/wav ></audio>"; 
字符串链接=”;

首先,确保音频文件已上载到服务器上

然后,表
tblfiles5
中的
voicename
值为仅为文件名

并且您应该在Literal1使用div。它表示HtmlGenericControl组件。你为什么使用文字1.文本

using (connection)
    {
        SqlCommand command = new SqlCommand(
          "select voicename from tblfiles5 where voicename='" + TextBox1.Text + "'",
          connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                    string link = "<audio controls> <Source src='"+ 
                    reader.GetString(0) + "' type ='audio/wav' ></audio>";
                    Literal1.InnerHtml = link;
            }
        }
        else
        {
                    MessageBox.Show("No "+ TextBox1.Text+" exist in database");
        }
        reader.Close();
    }
使用(连接)
{
SqlCommand=newsqlcommand(
“从TBLFile5中选择voicename,其中voicename=”+TextBox1.Text+“”,
连接);
connection.Open();
SqlDataReader=command.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
{
字符串链接=”;
Literal1.InnerHtml=链接;
}
}
其他的
{
MessageBox.Show(“否”+TextBox1.Text+“数据库中存在”);
}
reader.Close();
}
在aspx文件中:

          <div id="Literal1" runat="server">
          </div>


最重要的是,请继续阅读,因为您易受此攻击。在服务器环境中调用
MessageBox.Show
将无法按预期工作。MessageBox.Show正在工作,即当文本插入数据库时。如果输入的文本存在于数据库中,它将在MessageBox中返回。否则,将触发显示数据库“localhost”中不存在“+TextBox1.text+”的查询在开发人员的计算机上不是服务器环境。实际上,我正在测试我的数据库值是否在启动查询时被检索。如果我删除了,当我更改sql查询以选择voicename,id from tblfiles5,其中voicename='+TextBox1.Text+''时,我不知道写入的查询是否在数据库中运行“、con和string link=”“;你应该提供更多信息,不工作是什么意思?实际上,你的问题不太清楚。什么是文字1?文本框?标签?它应该在回发时更新,并在单击按钮后更改。如果你使用div组件,你需要InnerHtml而不是Literal1.text我更新了这个问题,希望它能让你clear@JeremyThompson不,我没有