把数据通过C#写入数据库的模板

来源:互联网 发布:ubuntu usb启动盘制作 编辑:程序博客网 时间:2024/05/21 09:30
using Ktkj.BusSchedule.DAL.MssqlDal;using Ktkj.BusSchedule.Infrastructure.Entity;using System;using System.Collections.Generic;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using System.Linq;using System.Text;  //dal层下基本都要引用的 在Dal层下写时要写上的namespace Ktkj.BusSchedule.DAL.Factory{    public class BaseOrganizationDal    {#if DEBUG        private static readonly string connString = "Server=218.70.87.86,2433;DataBase=JtTest;Uid=bustest;Pwd=abcd1234";        /*server:数据库服务器名,DataBase:数据库名,Uid:数据库账号,pwd:密码*/#else        private static string connString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();  #endif        /// <summary>        /// 查找所有的BaseOrganization        /// </summary>        /// <returns></returns>        public int AddBaseOrganization(Base_Organization baseOrg)        {            string commandText = "insert into Base_Organization " +                                 "(CreatedBy,CreatedOn,ModifiedBy,ModifiedOn,Code," +                                 "FullName,ShortName,Location,RegisterAddress,Contact," +                                 "DefaultLanguage,DefaultTheme,Effective) " +                                 "values " +                                 "(@CreatedBy,@CreatedOn,@ModifiedBy,@ModifiedOn,@Code," +                                 "@FullName,@ShortName,@Location,@RegisterAddress,@Contact," +                                 "@DefaultLanguage,@DefaultTheme,@Effective)";            SqlParameter[] pms = baseOrgToSqlParameter(baseOrg);            return SQLHelper.ExecuteNonQuery(connString, CommandType.Text, commandText, pms);        }        public SqlParameter[] baseOrgToSqlParameter(Base_Organization baseOrg)        {            SqlParameter[] pms = new SqlParameter[]            {                //new SqlParameter("@ID",SqlDbType.BigInt,0) {Value=baseOrg.ID==0?DBNull.Value:(object)baseOrg.ID },                new SqlParameter("@CreatedBy",SqlDbType.BigInt,0) {Value=baseOrg.CreatedBy==0?DBNull.Value:(object)baseOrg.CreatedBy },                new SqlParameter("@CreatedOn",SqlDbType.DateTime,50) {Value=DTExchange(baseOrg.CreatedOn) },                new SqlParameter("@ModifiedBy",SqlDbType.BigInt,53) {Value=baseOrg.ModifiedBy==0?DBNull.Value:(object)baseOrg.ModifiedBy },                new SqlParameter("@ModifiedOn",SqlDbType.DateTime,1) {Value=DTExchange(baseOrg.ModifiedOn) },                new SqlParameter("@Code",SqlDbType.VarChar,20) {Value=baseOrg.Code==null?DBNull.Value:(object)baseOrg.Code },                new SqlParameter("@FullName",SqlDbType.NVarChar,100) {Value=baseOrg.FullName==null?DBNull.Value:(object)baseOrg.FullName },                new SqlParameter("@ShortName",SqlDbType.NVarChar,20) {Value=baseOrg.ShortName==null?DBNull.Value:(object)baseOrg.ShortName },                new SqlParameter("@Location",SqlDbType.NVarChar,100) {Value=baseOrg.Location==null?DBNull.Value:(object)baseOrg.Location },                new SqlParameter("@RegisterAddress",SqlDbType.NVarChar,100) {Value=baseOrg.RegisterAddress==null?DBNull.Value:(object)baseOrg.RegisterAddress },                new SqlParameter("@Contact",SqlDbType.NVarChar,20) {Value=baseOrg.Contact==null?DBNull.Value:(object)baseOrg.Contact },                new SqlParameter("@DefaultLanguage",SqlDbType.SmallInt,1) {Value=baseOrg.DefaultLanguage==0?DBNull.Value:(object)baseOrg.DefaultLanguage },                new SqlParameter("@DefaultTheme",SqlDbType.SmallInt,20) {Value=baseOrg.DefaultTheme==0?DBNull.Value:(object)baseOrg.DefaultTheme },                new SqlParameter("@Effective",SqlDbType.TinyInt,0) {Value=baseOrg.Effective==0?DBNull.Value:(object)baseOrg.Effective },            };            return pms;        }        private object DTExchange(DateTime dt)        {            string min = SqlDateTime.MinValue.ToString();            string max = SqlDateTime.MaxValue.ToString();            DateTime dtmin = Convert.ToDateTime(min);            DateTime dtmax = Convert.ToDateTime(max);            return dt >= dtmin && dt <= dtmax ? (object)dt.ToString() : DBNull.Value;        }    }}
阅读全文
0 0
原创粉丝点击