我需要在Web服务中使用mysql数据库。
我使用wampserver在mysql中创建小型数据库,然后我将此数据库导出为DB1.sql
。
我将此文件添加到我的Web服务解决方案并添加一个方法来调用此数据库,我的数据库有一个表student
并且没有用户名和密码。
我写了以下代码:
namespace WebApplication8
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string conDb()
{
SqlConnection myConnection = new SqlConnection("database=DB1;");
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM student;", myConnection);
myCommand.ExecuteNonQuery();
return "pass";
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return "fail";
}
}
}
}
但是当我运行这个webservice并调用“conDB”函数时,它会返回给我“失败”。 那么问题出在哪里