object SQLparameterNull(object obj)
{
if(obj == null)
return DBNull.Value;
return obj;
}
object SQLparameterNull(string obj)
{
if (obj == null || obj=="")
return DBNull.Value;
return obj;
}
public int insertData(uint endpoint,string att,string whois)
{
string sql = "insert netsp.czipv4(endpoint,attribution,owner_whois)value("+endpoint+",\'"+att+"\',\'"+whois+"\');";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.Parameters.AddWithValue(@"owner_whois", SQLparameterNull(whois));
return cmd.ExecuteNonQuery();
}
public void updateData(uint endpoint, string att, string whois)
{
string sql = "update netsp.czipv4 set attribution=\'"+att+"\' ,owner_whois=\'"+whois+"\' where endpoint=\'"+endpoint+"\';";
MySqlCommand cmd = new MySqlCommand(sql, con);
cmd.Parameters.AddWithValue(@"owner_whois", SQLparameterNull(whois));
cmd.ExecuteNonQuery();
}
C#MySQL接口插入空值的实例。