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调用存储过程