Ejemplo n.º 1
0
 public object Get(int fieldIndex, object defaultValue, Type type)
 {
     if (fieldIndex < 0 || _reader.IsDBNull(fieldIndex))
     {
         return(defaultValue);
     }
     if (type.IsNullable())
     {
         if (_reader.IsDBNull(fieldIndex))
         {
             return(null);
         }
         type = type.GetGenericArguments()[0];
     }
     if (type.IsEnum)
     {
         type = typeof(int);
     }
     try
     {
         return(Convert.ChangeType(_reader[fieldIndex], type));
     }
     catch (Exception ex)
     {
         var msg = string.Format("DataReader failed to get field #{0} with type {1} as type {2}", fieldIndex, _reader.GetDataTypeName(fieldIndex), type.Name);
         throw new PriusException(msg, ex);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 备份指定的数据表。
        /// </summary>
        /// <param name="Connection">已经打开的数据库连接。</param>
        /// <param name="xtw">XML 书写器。</param>
        /// <param name="TableName">数据表名称。</param>
        private void BackupTable(System.Data.SqlClient.SqlConnection Connection, System.Xml.XmlTextWriter xtw, string TableName)
        {
            xtw.WriteStartElement(TableName);
            System.Data.SqlClient.SqlDataReader sdr  = null;
            System.Data.SqlClient.SqlCommand    comm = new System.Data.SqlClient.SqlCommand();
            comm.Connection = Connection;

            comm.CommandText = @"SELECT * FROM [" + TableName + "]";
            sdr = comm.ExecuteReader();

            try
            {
                for (int i = 0; i < sdr.FieldCount; i++)
                {
                    xtw.WriteAttributeString(sdr.GetName(i), sdr.GetDataTypeName(i));
                }
                while (sdr.Read())
                {
                    xtw.WriteStartElement("Row");
                    for (int i = 0; i < sdr.FieldCount; i++)
                    {
                        switch ((System.Data.SqlDbType)System.Enum.Parse(typeof(System.Data.SqlDbType), sdr.GetDataTypeName(i), true))
                        {
                        case System.Data.SqlDbType.BigInt:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Bit:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Date:
                        case System.Data.SqlDbType.DateTime:
                        case System.Data.SqlDbType.DateTime2:
                        case System.Data.SqlDbType.SmallDateTime:
                        case System.Data.SqlDbType.Time:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.DateTimeOffset:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Decimal:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Float:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Int:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Money:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Char:
                        case System.Data.SqlDbType.Text:
                        case System.Data.SqlDbType.VarChar:
                        case System.Data.SqlDbType.NChar:
                        case System.Data.SqlDbType.NText:
                        case System.Data.SqlDbType.NVarChar:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Real:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.SmallInt:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.SmallMoney:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.TinyInt:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.UniqueIdentifier:
                            xtw.WriteElementString(sdr.GetName(i), sdr[i].ToString());
                            break;

                        case System.Data.SqlDbType.Binary:
                            xtw.WriteElementString(sdr.GetName(i), System.Text.Encoding.UTF8.GetString(sdr.GetSqlBytes(i).Value));
                            break;

                        case System.Data.SqlDbType.VarBinary:
                            xtw.WriteElementString(sdr.GetName(i), System.Text.Encoding.UTF8.GetString(sdr.GetSqlBytes(i).Value));
                            break;

                        case System.Data.SqlDbType.Timestamp:
                            xtw.WriteElementString(sdr.GetName(i), System.Text.Encoding.UTF8.GetString(sdr.GetSqlBytes(i).Value));
                            break;

                        case System.Data.SqlDbType.Image:
                            xtw.WriteElementString(sdr.GetName(i), System.Text.Encoding.UTF8.GetString(sdr.GetSqlBytes(i).Value));
                            break;

                        case System.Data.SqlDbType.Structured:    //此类型操作待处理
                            break;

                        case System.Data.SqlDbType.Udt:    //此类型操作待处理
                            break;

                        case System.Data.SqlDbType.Variant:    //此类型操作待处理
                            break;

                        case System.Data.SqlDbType.Xml:    //此类型操作待处理
                            break;
                        }
                    }
                    xtw.WriteEndElement();
                }
            }
            finally
            {
                sdr.Close();
            }
            xtw.WriteEndElement();
        }