此消息的底部是该类的类和控制器。我必须为几百个SQL表做这个。呀。
我想做的是能够使用更通用的基本控制器,以便特定的控制器类似于:
public class IMS_ProductController : IMS_BaseController
public IEnumerable<IMS_Table> _recordset {get; set;}
string _tablename = "IMS_Product";
string _keyname = "ProductID";
}
我的基本控制器就像下面的IMS_ProductController。
问题是上面的第一行。如何照顾_recordset,更具体地说,&lt; IMS_Table&gt;每个SQL表都有所不同。您可以在下面的控制器中看到(我又想变成一个通用控制器)我做的事情:
var table = new List<IMS_Table>();
or
IMS_Table t = new IMS_Table();
这样的事情。
任何建议都会非常有用。
谢谢! 克里斯
namespace IMS.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Reflection;
using System.Linq;
//using System.Runtime.Serialization.Formatters.Binary;
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
[Serializable]
public class MappingAttribute : Attribute
{
public string ColumnName = null;
}
public class IMS_Product
{
[Mapping(ColumnName = "ProductID")]
[Key]
public Guid ProductId { get; set; }
[Mapping(ColumnName = "Name")]
[Required]
public string Name { get; set; }
[Mapping(ColumnName = "Description")]
public string Description { get; set; }
[Mapping(ColumnName = "PortalID")]
public int PortalID { get; set; }
[Mapping(ColumnName = "Smith_ProductID")]
public int Smith_ProductID { get; set; }
[Mapping(ColumnName = "IsDigital")]
public int IsDigital { get; set; }
[Mapping(ColumnName = "PublisherID")]
public long PublisherID { get; set; }
[Mapping(ColumnName = "Released")]
public DateTime Released { get; set; }
[Mapping(ColumnName = "Length")]
public long Length { get; set; }
[Mapping(ColumnName = "CreatedOn")]
public DateTime CreatedOn { get; set; }
[Mapping(ColumnName = "CreatedBy")]
public int CreatedBy { get; set; }
[Mapping(ColumnName = "ModifiedOn")]
public DateTime ModifiedOn { get; set; }
[Mapping(ColumnName = "ModifiedBy")]
public int ModifiedBy { get; set; }
[Mapping(ColumnName = "Url")]
[StringLength(283)]
public string Url { get; set; }
[Mapping(ColumnName = "dnnFileID")]
public int dnnFileID { get; set; }
[Mapping(ColumnName = "dnnFolderID")]
public int dnnFolderID { get; set; }
[Mapping(ColumnName = "TypeTagID")]
public Guid TypeTagID { get; set; }
}
public partial class IMS_Table : IMS_Product { }
public partial class IMS_ProductController
{
public IEnumerable<IMS_Table> _recordset {get; set;}
string _tablename = "IMS_Product";
string _keyname = "ProductID";
T MapToClass<T>(SqlDataReader reader) where T : class
{
T returnedObject = Activator.CreateInstance<T>();
List<PropertyInfo> modelProperties = returnedObject.GetType().GetProperties().OrderBy(p => p.MetadataToken).ToList();
for (int i = 0; i < modelProperties.Count; i++)
try
{
modelProperties[i].SetValue(returnedObject, Convert.ChangeType(reader.GetValue(i), modelProperties[i].PropertyType), null);
}
catch { }
return returnedObject;
}
public void gets(string keyval)
{
string sql = string.Format("SELECT * from {0} where {1}='{2}'", _tablename, _keyname, keyval);
getIt(sql);
}
public string gets(string keyval, string where)
{
string sql = string.Format("SELECT * from {0} where {1}='{2}' {3}", _tablename, _keyname, keyval, where);
try
{
getIt(sql);
return "Sucess: " + sql;
}
catch
{
return "Error: " + sql;
}
}
public void sets(string keyval,string field, string value)
{
setIt(keyval, field, value);
}
private void getIt(string strSQL)
{
var table = new List<IMS_Table>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString))
{
con.Open();
using (var cmd = new SqlCommand(strSQL, con))
{
IMS_Table t = new IMS_Table();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
t = MapToClass<IMS_Table>(reader);
table.Add(t);
}
_recordset = table;
reader.Close();
reader.Dispose();
}
cmd.Dispose();
}
con.Close();
con.Dispose();
}
}
private void setIt(string keyval, string field, string value)
{
var products = new List<IMS_Table>();
var strSQL = string.Format("update {0} set {1} = '{2}' where {3}='{4}'", _tablename, field, value, _keyname, keyval);
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["IMS"].ConnectionString))
{
con.Open();
SqlCommand cmd = new SqlCommand(strSQL, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
}
}
}
}
答案 0 :(得分:0)
将您的课程更改为public partial class IMS_ProductController<T> where T :new()
将MapToClass
更改为T MapToClass(IDataRecord record)
。 请注意DataReader
实施IDataRecord
。
将 IMS_Table 替换为 T 无处不在
答案 1 :(得分:0)
最终答案:
Traceback (most recent call last):
File "C:\Anaconda2\Lib\_nsis.py", line 164, in <module> main()
File "C:\Anaconda2\Lib\_nsis.py", line 150, in main
mk_menus(remove=False)
File "C:\Anaconda2\Lib\_nsis.py", line 94, in mk_menus
err("Traceback:\n%s\n" % traceback.format_exc(20))
IOError: [Errno 9] Bad file descriptor