該消息的底部是該類的類和控制器。我必須爲幾百個SQL表執行此操作。是啊。c#派生類問題
我想什麼做的是能夠使用更通用的基礎控制器,使得特定的控制器會是這樣的:
public class IMS_ProductController : IMS_BaseController
public IEnumerable<IMS_Table> _recordset {get; set;}
string _tablename = "IMS_Product";
string _keyname = "ProductID";
}
和我的基地位指示會像下面的IMS_ProductController。
問題是上面的第一行。我如何處理_recordset,更具體地說,<IMS_Table>對於每個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();
}
}
}
}
在繼續之前,就谷歌爲 「SQL注入」。 –
您可能會在某處使用'BaseController',但爲何不學習使用經過驗證的ORM庫?像實體框架或nHibernate? –
首先,檢查上面的評論。其次,你有沒有試圖自己做? –