数据库增加、更新、插入与读取(以及返回多个值out的使用——处理报错:控制离开当前方法之前必须对输出参数“XXX”赋值)

public static void getPreData(string workId, out string first_pressure, out string circle_wave, out string first_wave, out string circle_pressure)
        {
            try
            {
                //conn.Open();
                sql = "SELECT * FROM working_face where id=@id";
                cmd.Parameters.Add("@id", MySqlDbType.VarChar).Value = workId;
                dr = cmd.ExecuteReader();
                ==first_pressure = ""; first_wave = ""; circle_pressure = ""; circle_wave==
                if (dr == null) return;**//out输出返回前要带着初定义**
                dr.Read();
                string mine_height = dr.GetValue(32).ToString();
                string inclination = dr.GetValue(7).ToString();
                string mine_stratification = dr.GetValue(33).ToString();
                string layer_number = dr.GetValue(34).ToString();
                string bottom = dr.GetValue(35).ToString();
                string face_length = dr.GetValue(5).ToString();
                *此处涉及部分内部代码不可外漏*
                String content = dim.Result;
                String[] str = content.Split(',');//划分字符串
                first_pressure = str[9].ToString();
                first_wave = str[10].ToString();
                circle_pressure = str[11].ToString();
                circle_wave = str[12].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                first_pressure = ""; first_wave = ""; circle_pressure = ""; circle_wave = "";//out输出返回前要带着初定义
            }
            finally
            {
                myConn.Close();
            }
        }
        public static void afterReadSql(string workId, out string check)
        {
            try
            {
                check = "";
                //conn.Open();
                cmd = myConn.CreateCommand();
                cmd.CommandText = "select * from temporary_storage where workId=@workId";
                cmd.Parameters.Add("@workId", MySqlDbType.VarChar).Value = workId;
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    check = dr.GetValue(4).ToString();

                }
                else
                    Console.WriteLine("3333");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                check = "";
            }
            finally
            {
                myConn.Close();

            }
        }
        public static void thenUpdataData(string workId, string first_pressure, string first_wave, string circle_pressure, string circle_wave)
        {
            try
            {
                cmd = myConn.CreateCommand();
                cmd.CommandText = "update temporary_storage set first_pressure=@first_pressure,first_wave=@first_wave," +
                "circle_pressure=@circle_pressure,circle_wave=@circle_wave  where workId=@workId";
                cmd.Parameters.Add("@workId", MySqlDbType.VarChar).Value = workId;
                cmd.Parameters.Add("@first_pressure", MySqlDbType.VarChar).Value = first_pressure;
                cmd.Parameters.Add("@first_wave", MySqlDbType.VarChar).Value = first_wave;
                cmd.Parameters.Add("@circle_pressure", MySqlDbType.VarChar).Value = circle_pressure;
                cmd.Parameters.Add("@circle_wave", MySqlDbType.VarChar).Value = circle_wave;
                myConn.Open();//打开连接
                cmd.ExecuteNonQuery();//执行不是查询的sql语句  
                MessageBox.Show("更新成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConn.Close();//不管打开成功还是失败,都能关闭连接
            }
        }
        public static void thenInsertData(string workId, string first_pressure, string first_wave, string circle_pressure, string circle_wave)
        {
            try
            {
                cmd = myConn.CreateCommand();
                cmd.CommandText = "insert into temporary_storage values(@first_pressure,@first_wave,@circle_pressure,@circle_wave,@workId)";

                cmd.Parameters.Add("@first_pressure", MySqlDbType.VarChar).Value = first_pressure;
                cmd.Parameters.Add("@first_wave", MySqlDbType.VarChar).Value = first_wave;
                cmd.Parameters.Add("@circle_pressure", MySqlDbType.VarChar).Value = circle_pressure;
                cmd.Parameters.Add("@circle_wave", MySqlDbType.VarChar).Value = circle_wave;
                cmd.Parameters.Add("@workId", MySqlDbType.VarChar).Value = workId;
                myConn.Open();//打开连接
                cmd.ExecuteNonQuery();//执行不是查询的sql语句
                MessageBox.Show("存入数据成功!");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConn.Close();//不管打开成功还是失败,都能关闭连接
            }
        }
        public static void beforeDrawRead(string workId, out string pressure1, out string wave1, out string pressure_n, out string wave_n)
        {
            try
            {
                cmd = myConn.CreateCommand();
                cmd.CommandText = "select * from temporary_storage where workId=@workId";
                cmd.Parameters.Add("@workId", MySqlDbType.VarChar).Value = workId;
                myConn.Open();
                dr = cmd.ExecuteReader();
                pressure1 = ""; wave1 = ""; pressure_n = ""; wave_n = "";
                if (dr == null) return;
                dr.Read();
                pressure1 = dr.GetValue(0).ToString();
                wave1 = dr.GetValue(1).ToString();
                pressure_n = dr.GetValue(2).ToString();
                wave_n = dr.GetValue(3).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                pressure1 = ""; wave1 = ""; pressure_n = ""; wave_n = "";
            }
            finally
            {
                myConn.Close();//不管打开成功还是失败,都能关闭连接
            }
        }

out的使用

首先应当注意在try catch结构中,一定要对将要输出的多个变量进行初始定义,如果没有初始定义会报错如下:
控制离开当前方法之前必须对输出参数“XXX”赋值
初始定义在catch中与文中try中均要有,try中的定义要放在含有return之前!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值