C# 如何使用对象和模态的列表(list<;Object>;)将数据插入数据库?
大家好,我正在尝试使用asp.net中的mvc向数据库插入一些值。我听说在插入数据时最好使用对象。那么,如何通过类中设计的属性使用对象列表来实现这一点呢 有一门课叫C# 如何使用对象和模态的列表(list<;Object>;)将数据插入数据库?,c#,asp.net,sql-server,C#,Asp.net,Sql Server,大家好,我正在尝试使用asp.net中的mvc向数据库插入一些值。我听说在插入数据时最好使用对象。那么,如何通过类中设计的属性使用对象列表来实现这一点呢 有一门课叫 public class Customer { public string Name { get; set; } public string Company { get; set; } public int Telephone { get; set; } pub
public class Customer
{
public string Name { get; set; }
public string Company { get; set; }
public int Telephone { get; set; }
public string Email { get; set; }
public int Id { get; set; }
}
因此,我有另一个类,它只执行sql命令
DBAccess.cs
public List<Customer> AddCustomers(string spName)
{
List<Customer> customers = new List<Customer>();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
}
如何使用这种场景将数据插入回数据库?非常感谢您的帮助。在数据库中,您必须执行SQL insert命令才能插入数据库。因此,您可以尝试以下方法。此方法将获取列表和表名作为参数。循环内部反射用于在键值对列表中插入值
public static bool Insert(List<YourModel> datas, string table)
{
bool result = false;
List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>();
SqlConnection con = new SqlConnection("your connection string");
con.Open();
try
{
foreach (var data in datas)
{
values.Clear();
foreach (var item in data.GetType().GetProperties())
{
values.Add(new KeyValuePair<string, string>(item.Name, item.GetValue(data).ToString()));
}
string xQry = getInsertCommand(table, values);
SqlCommand cmdi = new SqlCommand(xQry, con);
cmdi.ExecuteNonQuery();
}
result = true;
}
catch(Exception ex)
{ throw ex; }
finally { con.Close(); }
return result;
}
公共静态bool插入(列表数据、字符串表)
{
布尔结果=假;
列表值=新列表();
SqlConnection con=newsqlconnection(“您的连接字符串”);
con.Open();
尝试
{
foreach(数据中的var数据)
{
value.Clear();
foreach(data.GetType().GetProperties()中的变量项)
{
Add(新的KeyValuePair(item.Name,item.GetValue(data.ToString()));
}
字符串xQry=getInsertCommand(表,值);
SqlCommand cmdi=新的SqlCommand(xQry,con);
cmdi.ExecuteNonQuery();
}
结果=真;
}
捕获(例外情况除外)
{throw ex;}
最后{con.Close();}
返回结果;
}
在下面的方法中,键值对列表将被传递给另一个函数以执行insert命令
private static string getInsertCommand(string table, List<KeyValuePair<string, string>> values)
{
string query = null;
query += "INSERT INTO " + table + " ( ";
foreach (var item in values)
{
query += item.Key;
query += ", ";
}
query = query.Remove(query.Length - 2, 2);
query += ") VALUES ( ";
foreach (var item in values)
{
if (item.Key.GetType().Name == "System.Int") // or any other numerics
{
query += item.Value;
}
else
{
query += "'";
query += item.Value;
query += "'";
}
query += ", ";
}
query = query.Remove(query.Length - 2, 2);
query += ")";
return query;
}
私有静态字符串getInsertCommand(字符串表,列表值)
{
字符串查询=null;
查询+=“插入到”+表+“(”;
foreach(值中的var项)
{
query+=item.Key;
查询+=“,”;
}
query=query.Remove(query.Length-2,2);
查询+=”)值(”;
foreach(值中的var项)
{
if(item.Key.GetType().Name==“System.Int”)//或任何其他数字
{
查询+=项.值;
}
其他的
{
查询+=“'”;
查询+=项.值;
查询+=“'”;
}
查询+=“,”;
}
query=query.Remove(query.Length-2,2);
查询+=”;
返回查询;
}
是更新还是插入?数据表可以很容易地保存在数据库中SqlBulkCopy@Sami将数据插入数据库:)@Ashanelvis。看到这个,你可以为它编写一个存储过程insert@AwaisMahmood我想知道,如果我们可以使用像List
private static string getInsertCommand(string table, List<KeyValuePair<string, string>> values)
{
string query = null;
query += "INSERT INTO " + table + " ( ";
foreach (var item in values)
{
query += item.Key;
query += ", ";
}
query = query.Remove(query.Length - 2, 2);
query += ") VALUES ( ";
foreach (var item in values)
{
if (item.Key.GetType().Name == "System.Int") // or any other numerics
{
query += item.Value;
}
else
{
query += "'";
query += item.Value;
query += "'";
}
query += ", ";
}
query = query.Remove(query.Length - 2, 2);
query += ")";
return query;
}