使用C#在mysql数据库中添加数据

时间:2013-09-29 11:13:25

标签: mysql asp.net

我是第一次使用mysql作为ASP.NET应用程序的后端 我试图将数据插入表中,但我一直有这个错误。 我已经安装了mysql连接器。有人可以告诉我哪里做错了。 这是错误:

MySql.Data.MySqlClient.MySqlException was unhandled by user code
  HResult=-2147467259
  Message=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
  Source=MySql.Data
  ErrorCode=-2147467259
  Number=1064
  StackTrace:
       at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
       at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
       at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
       at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
       at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
       at Agohealth.Requestlab.btnsubmit_Click(Object sender, EventArgs e) in C:\Users\PC3\Documents\Visual Studio 2010\Projects\Agohealth\Agohealth\LabRequest.aspx.cs:line 38
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace hiredad
{
public partial class Requestlab : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        string connectionstring =   ConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString;

        using (MySqlConnection conn = new MySqlConnection(connectionstring))
        {
            MySqlCommand cmd = new MySqlCommand();
            conn.CreateCommand();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandText=("INSERT INTO labrequest (Examination,TestName,RequestCode,PatientNumber,Date,Doctor)"
            + ("VALUES (?Examination,?TestName,?RequestCode,?PatientNumber,?Date,?Doctor"));
            cmd.Parameters.Add("?Examination", MySqlDbType.VarChar).Value = DropDownList1.SelectedValue;
            cmd.Parameters.Add("?TestName", MySqlDbType.VarChar).Value = dlistlabtestname.SelectedValue;
            cmd.Parameters.Add("?RequestCode", MySqlDbType.VarChar).Value = txtrequestcode.Text;
            cmd.Parameters.Add("?PatientNumber", MySqlDbType.VarChar).Value = txtreginum.Text;
            cmd.Parameters.Add("?Date", MySqlDbType.Date).Value = labdate.Text;
            cmd.Parameters.Add("?Doctor", MySqlDbType.VarChar).Value = txtdoctor.Text;
            cmd.ExecuteNonQuery();
            Message.Text = "Sucessfully  inserted!";
        }
    }
}

0 个答案:

没有答案