有人可以教我如何使用这个sql包装器吗?我很困惑在第二个参数参数上放什么... thanks =)
public void Insert(string strSQL, List<MySqlParameter> params)
{
if(this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(strSQL, connection)
foreach(MySqlParameter param in params)
cmd.Parameters.Add(param);
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
答案 0 :(得分:2)
您可以像这样使用它,
List<MySqlParameter> sqlParams = new List<MySqlParameter>();
SqlParameter param1 = new SqlParameter();
param.ParameterName = "@City";
param.Value = inputCity;
SqlParameter param2 = new SqlParameter();
param.ParameterName = "@Country";
param.Value = inputCountry;
sqlParams.Add(param1);
sqlParams.Add(param2);
Insert("SELECT * FROM customers WHERE city = @City AND country = @Country", sqlParams);
答案 1 :(得分:1)
听起来(评论)问题就是c#调用代码,在这种情况下dapper-dot-net可能会有所帮助 - 它是一个高度优化的实用程序层,使调用更容易,例如:
int id = 123;
string name = "Fred";
connection.Execute(@"insert into TableName (Id, Name) values (?id, ?name)",
new {id, name});
它为您处理所有命令和参数代码,因此您只需担心连接,SQL和值。还有类似的方法(Query[<T>]
等)用于读取数据。例如:
int customerId = 12345;
List<Order> orders = connection.Query<Order>(@"
select * from Orders where CustomerId = ?customerId", new {customerId})
.ToList();
答案 2 :(得分:0)
以下是示例代码。
List<MySqlParameter> parameters = new List<MySqlParameter>();
string sql = "INSERT INTO table1 VALUES(?Name, ?Age)";
parameters.Add(new MySqlParameter() { ParameterName = "?Name", Value = "Mahan"});
parameters.Add(new MySqlParameter() { ParameterName = "?Age", Value = "??"});
Insert(sql, parameters);