插入查询错误

时间:2012-04-10 11:46:34

标签: c# asp.net mysql

我的代码主要从文件中读取一些数据并将其保存在mySql数据库中。我使用了insert sql查询,然后使用select sql查询来读取我数据库中的所有数据。

运行此代码时,我收到错误“Key not not null。\ r \ nParameter name:key} System.Exception {System.ArgumentNullException}”

我该如何解决这个问题?

private void bindGrid()//连接数据库的函数         {

        string DocID = User.Identity.Name.ToString();

        string connectionString = "server=127.0.0.1;"
                                  + "database=hospital;"
                                   + "uid=root;"
                                   + "pwd=missy3asoola;";

        MySqlConnection conn = new MySqlConnection(connectionString);

        try
        {
            conn.Open();
            StreamReader objReader = new StreamReader("D:/Senior/WebApplication-metbhdelaXD/WebApplication2/Textfile.txt");
            string sLine = "";
            ArrayList arrText = new ArrayList();

            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                    arrText.Add(sLine);
            }
            objReader.Close();

            MySqlCommand cmdread = new MySqlCommand();
            cmdread.Connection = conn;
            //Defining Mysql Parameters
            MySqlParameter Temperature, Pulserate, Timestamp, Doctor_idDoctor, Personal_PatientID;
            Temperature = new MySqlParameter();
            Pulserate = new MySqlParameter();
            Timestamp = new MySqlParameter();
            Doctor_idDoctor = new MySqlParameter();
            Personal_PatientID = new MySqlParameter();
            //Defining Types
            Temperature.MySqlDbType = MySqlDbType.Int32;
            Pulserate.MySqlDbType = MySqlDbType.Int32;
            Timestamp.MySqlDbType = MySqlDbType.Timestamp;
            Doctor_idDoctor.MySqlDbType = MySqlDbType.Int32;
            Personal_PatientID.MySqlDbType = MySqlDbType.Int32;
            //Defining values
            Temperature.Value = arrText[0];
            Pulserate.Value = arrText[1];
            Timestamp.Value = arrText[2];
            Doctor_idDoctor.Value = arrText[3];
            Personal_PatientID.Value = arrText[4];

            //cmdread.Parameters.Clear();
            //Adding parameters
            cmdread.Parameters.Add(Temperature);
            cmdread.Parameters.Add(Pulserate);
            cmdread.Parameters.Add(Timestamp);
            cmdread.Parameters.Add(Doctor_idDoctor);
            cmdread.Parameters.Add(Personal_PatientID);
            cmdread.Connection = conn;
            cmdread.CommandText = "INSERT INTO medical(Temperature,`Pulse rate`,Timestamp,Doctor_idDoctor,Personal_PatientID)VALUES([@Temperature],[@`Pulse rate`],[@Timestamp],[@Doctor_idDoctor],[@Personal_PatientID]);";
            cmdread.CommandType = CommandType.Text;

            cmdread.ExecuteNonQuery();

            cmdread.Dispose();


            string sqlQuery1 = "Select * from medical where Doctor_idDoctor=" + DocID;

            MySqlCommand cmdmedical = new MySqlCommand(sqlQuery1, conn);

            GridView1.DataSource = cmdmedical.ExecuteReader() ;  //ex reader for retrieving
            GridView1.DataBind();
            conn.Close();

        }
        catch (Exception ex)
        {
            Response.Write(ex.StackTrace);
        }



    }

1 个答案:

答案 0 :(得分:3)

您没有将文本文件中的值应用于参数。如果你不这样做,并且你的列是一个不可为空的列,那么你将得到一个例外,这就是这里发生的事情。

您应将其更改为:

cmdread.Parameters.Add("@Temperature",MySqlDbType.Int32, 5).Value = //value from text file;
cmdread.Parameters.Add("@Pulse rate", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("@Timestamp", MySqlDbType.Timestamp, 15).Value = //value from text file;
cmdread.Parameters.Add("@Doctor_idDoctor", MySqlDbType.Int32, 3).Value = //value from text file;
cmdread.Parameters.Add("@Personal_PatientID", MySqlDbType.Int32, 3).Value = //value from text file;

另外,尽量不要在列名和参数名中使用空格。我会将@Pulse rate更改为@PulseRate


此外,我不明白为什么要将cmdread.ExecuteNonQuery()的返回值作为DataSource提供给GridView。这似乎不对。