Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 插入查询错误_C#_Asp.net_Mysql - Fatal编程技术网

C# 插入查询错误

C# 插入查询错误,c#,asp.net,mysql,C#,Asp.net,Mysql,我的代码主要从文件中读取一些数据,并将其保存在mySql数据库中。我使用insert sql查询,然后使用select sql查询读取数据库中的所有数据 运行此代码时,我收到错误“Key不能为null。\r\n参数名称:Key}System.Exception{System.ArgumentNullException}” 我该如何解决这个问题 私有void bindGrid()//连接到数据库的函数 { string DocID = User.Identity.Name.ToSt

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

运行此代码时,我收到错误“Key不能为null。\r\n参数名称:Key}System.Exception{System.ArgumentNullException}”

我该如何解决这个问题

私有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);
        }



    }

您没有将文本文件中的值应用于参数。如果您不这样做,并且您的列是不可为空的列,那么您将得到一个异常,这正是此处发生的情况

您应该将其更改为:

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()的返回值
作为GridView的数据源。这似乎不正确。

您能确保Textfile.txt?中的温度列值不是空的吗?看起来温度列值是作为空值或空值传递的。这是我的文本文件37 68 2/25/1988 9:00 2 101的第一行,我必须保留空间,因为这是它的工作方式在数据库中写入。很抱歉,我不明白,如果我要在这里写入文本文件的值..那么从文本文件中读取有什么用呢???@Mino这取决于文本文件的格式,所以我在这里帮不上忙。但是你需要将值应用到参数中。这就是为什么你会得到NULL异常。SQL命令不仅仅会神奇地解释文本文件中的值。你需要设置它们。好的,这是我文本文件中的部分数据:37 68 2/25/1988 9:00 2 101我应该在代码中为温度写37吗??