vs 中怎么把sql数据库中的数据存入到c#中的数组里,求详细解答!
1个回答
展开全部
首先,通过SQL语言把数据读入Dataset中,再用反射吧Dataset中的数据存入数组(其使用对象更好一些),给你个例子:
数据查询:
//数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.
public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["SQLString"] + ";UTF8Encoding=True;Pooling=True;Max Pool Size=100;";
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString) {
using(SQLiteConnection connection = new SQLiteConnection(connectionString)) {
DataSet ds = new DataSet();
try {
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
} catch (System.Data.SQLite.SQLiteException ex) {
connection.Close();
throw new Exception(ex.Message);
} finally {
connection.Close();
}
return ds;
}
}
数据的转换(Dataset -> 数据对象):
private static ArrayList DataSetConvertToObject(DataSet ds, Type objectType) {
ArrayList objList = new ArrayList();
for (int j = 0; j < ds.Tables[0].Rows.Count; j++) {
Object obj = Activator.CreateInstance(objectType);
PropertyInfo[] properties = obj.GetType().GetProperties();
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) {
string columnName = ds.Tables[0].Columns[i].Caption;
string value = ds.Tables[0].Rows[j][columnName].ToString();
foreach(PropertyInfo info in properties) {
if (info.Name.Equals(columnName)) {
string propertyType = info.PropertyType.Name;
switch (propertyType) {
case "Guid":
{
info.SetValue(obj, new Guid(value), null);
break;
}
case "DateTime":
{
DateTimeFormatInfo format = new CultureInfo("zh-cn", true).DateTimeFormat;
info.SetValue(obj, DateTime.Parse(value, format), null);
break;
}
case "String":
default:
{
info.SetValue(obj, value, null);
break;
}
case "Int32":
{
info.SetValue(obj, int.Parse(value), null);
break;
}
case "Single":
{
info.SetValue(obj, float.Parse(value), null);
break;
}
}
break;
}
}
}
objList.Add(obj);
}
return objList;
}
数据查询:
//数据库连接字符串(web.config来配置),可以动态更改SQLString支持多数据库.
public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["SQLString"] + ";UTF8Encoding=True;Pooling=True;Max Pool Size=100;";
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString) {
using(SQLiteConnection connection = new SQLiteConnection(connectionString)) {
DataSet ds = new DataSet();
try {
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
} catch (System.Data.SQLite.SQLiteException ex) {
connection.Close();
throw new Exception(ex.Message);
} finally {
connection.Close();
}
return ds;
}
}
数据的转换(Dataset -> 数据对象):
private static ArrayList DataSetConvertToObject(DataSet ds, Type objectType) {
ArrayList objList = new ArrayList();
for (int j = 0; j < ds.Tables[0].Rows.Count; j++) {
Object obj = Activator.CreateInstance(objectType);
PropertyInfo[] properties = obj.GetType().GetProperties();
for (int i = 0; i < ds.Tables[0].Columns.Count; i++) {
string columnName = ds.Tables[0].Columns[i].Caption;
string value = ds.Tables[0].Rows[j][columnName].ToString();
foreach(PropertyInfo info in properties) {
if (info.Name.Equals(columnName)) {
string propertyType = info.PropertyType.Name;
switch (propertyType) {
case "Guid":
{
info.SetValue(obj, new Guid(value), null);
break;
}
case "DateTime":
{
DateTimeFormatInfo format = new CultureInfo("zh-cn", true).DateTimeFormat;
info.SetValue(obj, DateTime.Parse(value, format), null);
break;
}
case "String":
default:
{
info.SetValue(obj, value, null);
break;
}
case "Int32":
{
info.SetValue(obj, int.Parse(value), null);
break;
}
case "Single":
{
info.SetValue(obj, float.Parse(value), null);
break;
}
}
break;
}
}
}
objList.Add(obj);
}
return objList;
}
追问
百分的感谢!好人
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询