• C# 中的数据库操作~存储过程篇Mysql SqlServer


    Mysql

    存储过程查询方式:

      public  NetPort GetNetdevicePortName(string uuid)
            {
                var ret = new NetPort();
                try
                {
    
                    MySqlDataReader myreader = null;
                    MySqlCommand myCommand = null;
    
    
                    using (MySqlConnection connection = new MySqlConnection(_connectionMysqlString))
                    {
    
                        connection.Open();
                        myCommand = new MySqlCommand();
                        myCommand.Connection = connection;
                        myCommand.CommandText = "Pro_GetNetdevice_Typename";
                        myCommand.CommandType = CommandType.StoredProcedure;
                        MySqlParameter uuidParameter = new MySqlParameter("?uuid", MySqlDbType.String);//mysql的存储过程参数是以?打头的!!!!
                        uuidParameter.Value = uuid;
                        myCommand.Parameters.Add(uuidParameter);
                        myreader=myCommand.ExecuteReader();
                        List<string> PortNames = new List<string>();
                        List<string> FeatureIds = new List<string>();
                        while (myreader.Read())
                        {
                            PortNames.Add(myreader["type_name"].ToString());
                            FeatureIds.Add(myreader["featureid"].ToString());
                        }
                        ret.PortNames = PortNames;
                        ret.FeatureIds = FeatureIds;
                        return ret;
                    }
    
                }
                catch (Exception ex)
                {
                    _log.Error("查询端口失败" + ex);
                    return null;
                }
            }  

    SQL server

    普通数据库操作

        var ret = new List<ChartConfig>();
                try
                {
    
                    using (SqlConnection conn=DbHelper.GetConnection())
                    {
                        conn.Open();
                    string sql = "SELECT * FROM  Netdevice_DetailView_Config where Category=@category";
                        SqlCommand command = conn.CreateCommand();
                        command.CommandType = CommandType.Text;
                        command.CommandText = sql;
                        command.Parameters.Add("@Category",SqlDbType.VarChar);
                        command.Parameters["@Category"].Value = category;       
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {                      
                              var Node = new ChartConfig
                            {
                                Category = reader["Category"].ToString(),
                                FeatureIds = Array.ConvertAll(reader["FeatureId"].ToString().Split(','), new Converter<string, int>(int.Parse)),
                                FeatureNames = reader["FeatureName"].ToString().Split(','),
                                Title = reader["TitleFormat"].ToString()
                            };
                              ret.Add(Node);              
                        }
                        return ret;
                    }
    
                }
                catch (Exception ex)
                {
                    _log.Error("查询网络设备配置数据失败" + ex);
                    return null;
                }  

    EF 调用SQL SERVER存储过程

      using (var ctx = new busContext())
                {
                    SqlParameter[] para = new SqlParameter[] {
                    new SqlParameter("@in_station_id",1),
                    new SqlParameter("@in_register_type",2)
                    };
                    var busDetails = ctx.Database.SqlQuery<BusDetail>("EXEC [dbo].[bus_message] @in_station_id,@in_register_type", para);
                    Console.WriteLine(busDetails.First().sname);
                    Console.ReadLine();
                }

    待更新,mysql普通操作,sqlserver调用存储过程

  • 相关阅读:
    锋利的jQuery第2版学习笔记4、5章
    锋利的jQuery第2版学习笔记1~3章
    关于盒模型的理解
    CSS3秘笈第三版涵盖HTML5学习笔记13~17章
    CSS3秘笈第三版涵盖HTML5学习笔记9~12章
    CSS3秘笈第三版涵盖HTML5学习笔记6~8章
    CSS3秘笈第三版涵盖HTML5学习笔记1~5章
    锁(1):spin_lock & mutex_lock的区别? .
    休眠(1):sleep和wait的区别
    C++(1):error: invalid conversion from ‘void (*)()’ to ‘void (*)(int)
  • 原文地址:https://www.cnblogs.com/pzqu/p/9457619.html
Copyright © 2020-2023  润新知