zoukankan      html  css  js  c++  java
  • ADO.NET读书笔记

    1、数据库中的数据类型不区分值类型和引用类型。

    2、处理Null值

    对于从包含null值的列中获取数据,可以有两种方法在访问它之前检测他。

    <1>DBNull.Value

    如果数据库值为null,它将被设置为DBNull.Value。

    例子如下:

                      while (rd.Read ())
                        {
                            object t = rd[0];
                            if(t!=DBNull .Value  )
                            Console.WriteLine("{0}",(int)rd [0]);
                        }

    <2>使用IsDBNull方法

    SqlDataReader公开一个IsDBNull方法。在提取一个列的内容之前,使用这一方法来确定此列中是否包含Null值。

                       while (rd.Read ())
                        {
                           if(!rd.IsDBNull (0))
                            Console.WriteLine("{0}",(int)rd [0]);
                        }

    3、.net数据类型与数据库中数据类型匹配问题

    <1>首先要注意几个数据类型:Sql server Decimal可容纳的数据多于.Net Decimal 。

    sql server DateTime 范围比.Net DateTime 小。

    另外要保持.net 中string的长度不能超过sql server 中string的长度。

    <2>sqlTypes:在获取查询结果时,sqltypes用来匹配其相应sql server数据类型。为那些不能转换为.net数据类型的数据提供帮助,它是可空值类型,可以存储数据库中的int类型的null值。

           图:sqltypes的结构

    <3>SqlDbType指定要用于 SqlParameter 中的字段和属性的 SQL Server 特定的数据类型。

    连接字符串和配置文件 (ADO.NET)

    .NET Framework 4
     
    此内容为质量更高的人工翻译。若想同时查看此页面和原始英文页面的内容,请单击“首选项”然后选择“经典视图”作为您的查看首选项。

    在应用程序代码中嵌入连接字符串可能导致安全漏洞和维护问题。使用 Ildasm.exe(MSIL 反汇编程序) 工具可以查看编译到应用程序源代码中的未加密连接字符串。此外,如果连接字符串发生更改,则必须重新编译应用程序。因此,我们建议您将连接字符串存储在应用程序配置文件中。

    使用应用程序配置文件

    应用程序配置文件包含特定应用程序特有的设置。例如,ASP.NET 应用程序能包含一个或多个 web.config 文件,Windows 应用程序可能包含一个可选 app.config 文件。虽然配置文件的名称和位置会因应用程序宿主的不同而有所不同,但配置文件可以有相同的元素。

    connectionStrings 节

    连接字符串可作为键/值对存储在应用程序配置文件 configuration 元素的 connectionStrings 节中。子元素包括 addclearremove

    下面的配置文件片段演示用于存储连接字符串的架构和语法。name 属性是您提供用来唯一标识连接字符串的名称,以便在运行时可以检索到该字符串。providerName 是 .NET Framework 数据提供程序的固定名称,此名称在 machine.config 文件中注册。

    <?xml version='1.0' encoding='utf-8'?>
      <configuration>
        <connectionStrings>
          <clear />
          <add name="Name" 
           providerName="System.Data.ProviderName" 
           connectionString="Valid Connection String;" />
        </connectionStrings>
      </configuration>
    
    
    说明说明

    您可以将连接字符串的一部分保存在配置文件中,并使用 DbConnectionStringBuilder 类在运行时将该字符串补充完整。如果您预先不知道连接字符串的元素,或者不希望将敏感信息保存到配置文件中,这一方法会很有用。有关更多信息,请参见连接字符串生成器 (ADO.NET)

    使用外部配置文件

    外部配置文件是单独的文件,此类文件包含由一部分组成的配置文件的片段。外部配置文件由主配置文件引用。如果在部署完应用程序后连接字符串可能会被编辑,那么将 connectionStrings 节存储在物理上独立的文件中会很有用。例如,标准 ASP.NET 行为是在修改配置文件后重新启动应用程序域,这将导致状态信息丢失。然而,修改外部配置文件不会导致重新启动应用程序。外部配置文件并不局限于由 ASP.NET 使用,Windows 应用程序也可以使用。此外,文件访问安全性和权限也可以用于限制对外部配置文件的访问。运行时使用外部配置文件是透明的,且不需要特殊编码。

    若要将连接字符串存储在外部配置文件中,请创建一个只包含 connectionStrings 节的单独的文件。不要包含任何其他的元素、节或属性。此示例演示外部配置文件的语法。

      <connectionStrings>
        <add name="Name" 
         providerName="System.Data.ProviderName" 
         connectionString="Valid Connection String;" />
      </connectionStrings>
    
    

    在主应用程序配置文件中,您可以使用 configSource 属性指定外部文件的完全限定名和位置。此示例引用名为 connections.config 的外部配置文件。

    <?xml version='1.0' encoding='utf-8'?>
    <configuration>
        <connectionStrings configSource="connections.config"/>
    </configuration>
    
    
    运行时检索连接字符串

    .NET Framework 2.0 在 System.Configuration 命名空间中引入了新类,以简化在运行时从配置文件检索连接字符串的操作。您可以以编程方式按名称或提供程序名称检索连接字符串。

    说明说明

    machine.config 文件还包含 connectionStrings 节,此节包含 Visual Studio 使用的连接字符串。当按提供程序名称从 Windows 应用程序中的 app.config 文件检索连接字符串时,首先加载 machine.config 中的连接字符串,然后加载 app.config 中的项。connectionStrings 元素删除所有继承自内存中数据结构的引用后将立即添加 clear,以便只考虑在本地 app.config 文件中指定的连接字符串。

    使用配置类

    从 .NET Framework 2.0 开始,当使用本地计算机上的配置文件时,将使用 ConfigurationManager,从而替换已不推荐使用的 ConfigurationSettingsWebConfigurationManager 与 ASP.NET 配置文件一起使用。该管理器可以使用 Web 服务器上的配置文件,并允许以编程方式访问配置文件节(如 system.web)。

    说明说明

    在运行时访问配置文件需要对调用方授予权限,根据应用程序类型、配置文件和位置确定所需的权限。有关更多信息,请参见使用配置类WebConfigurationManager(对于 ASP.NET 应用程序),以及 ConfigurationManager(对于 Windows 应用程序)。

    您可以使用 ConnectionStringSettingsCollection 从应用程序配置文件中检索连接字符串。它包含 ConnectionStringSettings 对象的集合,每个对象表示 connectionStrings 节中的一项。它的属性 (Property) 映射为连接字符串属性 (Attribute),从而允许您通过指定名称或提供程序名称来检索连接字符串。

     

    属性

    说明

    Name

    连接字符串的名称。映射为 name 属性。

    ProviderName

    完全限定提供程序名。映射为 providerName 属性。

    ConnectionString

    连接字符串。映射为 connectionString 属性。

    示例:列出所有连接字符串

    此示例循环访问 ConnectionStringSettings 集合,并在控制台窗口中显示 NameProviderNameConnectionString 属性。

    说明说明

    System.Configuration.dll 并不包含在所有项目类型中,您可能需要对其设置引用才能使用配置类。特定应用程序配置文件的名称和位置因应用程序类型和宿主进程的不同而有所不同。

    using System.Configuration;
    
    class Program
    {
        static void Main()
        {
            GetConnectionStrings();
            Console.ReadLine();
        }
    
        static void GetConnectionStrings()
        {
            ConnectionStringSettingsCollection settings =
                ConfigurationManager.ConnectionStrings;
    
            if (settings != null)
            {
                foreach(ConnectionStringSettings cs in settings)
                {
                    Console.WriteLine(cs.Name);
                    Console.WriteLine(cs.ProviderName);
                    Console.WriteLine(cs.ConnectionString);
                }
            }
        }
    }
    
    
    

    示例:按名称检索连接字符串

    此示例演示如何通过指定连接字符串名称从配置文件中检索连接字符串。此代码创建一个 ConnectionStringSettings 对象,以将提供的输入参数与 ConnectionStrings 名称相匹配。如果未找到匹配名称,则该函数返回 null(在 Visual Basic 中返回 Nothing)。

    // Retrieves a connection string by name.
    // Returns null if the name is not found.
    static string GetConnectionStringByName(string name)
    {
        // Assume failure.
        string returnValue = null;
    
        // Look for the name in the connectionStrings section.
        ConnectionStringSettings settings =
            ConfigurationManager.ConnectionStrings[name];
    
        // If found, return the connection string.
        if (settings != null)
            returnValue = settings.ConnectionString;
    
        return returnValue;
    }
    
    
    

    示例:按提供程序名称检索连接字符串

    此示例演示如何通过指定 System.Data.ProviderName 格式的提供程序固定名称来检索连接字符串。此代码循环访问 ConnectionStringSettingsCollection,并返回找到的第一个 ProviderName 的连接字符串。如果未找到提供程序名称,则该函数返回 null(在 Visual Basic 中返回 Nothing)。

    // Retrieve a connection string by specifying the providerName.
    // Assumes one connection string per provider in the config file.
    static string GetConnectionStringByProvider(string providerName)
    {
        // Return null on failure.
        string returnValue = null;
    
        // Get the collection of connection strings.
        ConnectionStringSettingsCollection settings =
            ConfigurationManager.ConnectionStrings;
    
        // Walk through the collection and return the first 
        // connection string matching the providerName.
        if (settings != null)
        {
            foreach (ConnectionStringSettings cs in settings)
            {
                if (cs.ProviderName == providerName)
                    returnValue = cs.ConnectionString;
                break;
            }
        }
        return returnValue;
    }
    
    
    
    使用受保护配置加密配置文件节

    ASP.NET 2.0 引入了一个称为“受保护配置”的新功能,您可以通过此功能来加密配置文件中的敏感信息。虽然受保护配置主要是为 ASP.NET 应用程序设计的,但它也可以用于加密 Windows 应用程序中的配置文件节。有关受保护配置功能的详细说明,请参见使用受保护的配置加密配置信息

    下面的配置文件片断显示了加密后的 connectionStrings 节。configProtectionProvider 可指定用于加密和解密连接字符串的受保护配置提供程序。EncryptedData 节包含密文。

    <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
      <EncryptedData>
        <CipherData>
          <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAH2... </CipherValue>
        </CipherData>
      </EncryptedData>
    </connectionStrings>
    
    

    在运行时检索加密连接字符串时,.NET Framework 会使用指定的提供程序来解密 CipherValue 并将其提供给应用程序。您无需额外编写任何代码来管理解密过程。

    受保护配置提供程序

    受保护配置提供程序是在本地计算机的 machine.config 文件的 configProtectedData 节中注册的(如下面的片断所示),此片断演示 .NET Framework 附带的两个受保护配置提供程序。此处显示的值已被截断,以便于阅读。

    <configProtectedData defaultProvider="RsaProtectedConfigurationProvider">
      <providers>
        <add name="RsaProtectedConfigurationProvider" 
          type="System.Configuration.RsaProtectedConfigurationProvider, ... />
        <add name="DataProtectionConfigurationProvider" 
          type="System.Configuration.DpapiProtectedConfigurationProvider, ... />
      </providers>
    </configProtectedData>
    
    

    可以配置其他受保护配置提供程序,方法是将它们添加到 machine.config 文件中。还可以通过从 ProtectedConfigurationProvider 抽象基类继承来创建自己的受保护配置提供程序。下表描述了 .NET Framework 附带的两个配置文件。

     

    提供程序

    说明

    RSAProtectedConfigurationProvider

    使用 RSA 加密算法来加密和解密数据。RSA 算法既可用于公钥加密,也可用于数字签名。它还称为“公共密钥”或非对称加密,因为它使用两个不同的密钥。可以使用 ASP.NET IIS 注册工具 (Aspnet_regiis.exe) 来加密 Web.config 文件中的节和管理加密密钥。ASP.NET 在处理配置文件时解密该文件。ASP.NET 应用程序的标识必须对用于加密和解密各加密节的加密密钥具有读取权限。

    DPAPIProtectedConfigurationProvider

    使用 Windows Data Protection API (DPAPI) 来加密配置节。它使用 Windows 内置加密服务,并可为计算机特定或用户帐户特定保护进行配置。对于同一服务器上需要共享信息的多个应用程序来说,计算机特定保护非常有用。用户帐户特定保护可与以特定用户标识运行的服务(如共享宿主环境)一起使用。每个应用程序以单独的标识运行,这样就限制了对文件和数据库等资源的访问。

    这两种提供程序都可以对数据进行强加密。但是,如果计划在多台服务器(如网络场)上使用相同的加密配置文件,则只有通过 RsaProtectedConfigurationProvider 才能导出用于加密数据的加密密钥,并将其导入其他服务器。有关更多信息,请参见导入和导出受保护的配置 RSA 密钥容器

    使用配置类

    System.Configuration 命名空间提供以编程方式使用配置设置的类。ConfigurationManager 类可提供对计算机、应用程序和用户配置文件的访问。如果要创建 ASP.NET 应用程序,可以使用 WebConfigurationManager 类,它可提供相同的功能,同时还允许您访问 ASP.NET 应用程序特有的设置(如 <system.web> 中的设置)。

    说明说明

    System.Security.Cryptography 命名空间包含提供用于加密和解密数据的其他选项的类。如果需要采用在使用受保护配置时不可用的加密服务,请使用这些类。一些类是非托管 Microsoft CryptoAPI 的包装类,而其他类则是纯托管实现。有关更多信息,请参见Cryptographic Services

    App.config 示例

    此示例演示如何对 Windows 应用程序的 app.config 文件中的 connectionStrings 节的加密进行切换。在此示例中,该过程将应用程序的名称(例如“MyApplication.exe”)作为一个参数。然后,将对 app.config 文件进行加密,并采用名称“MyApplication.exe.config”将其复制到包含可执行文件的文件夹中。

    说明说明

    只能在加密连接字符串的计算机上对其进行解密。

    代码使用 OpenExeConfiguration 方法打开 app.config 文件以进行编辑,GetSection 方法返回 connectionStrings 节。然后,代码检查 IsProtected 属性,调用 ProtectSection 以加密未加密的节。调用 UnProtectSection() 方法以加密该节。Save 方法完成该操作并保存所做更改。

    说明说明

    必须在要运行代码的项目中设置对 System.Configuration.dll 的引用。

    static void ToggleConfigEncryption(string exeConfigName)
    {
        // Takes the executable file name without the
        // .config extension.
        try
        {
            // Open the configuration file and retrieve 
            // the connectionStrings section.
            Configuration config = ConfigurationManager.
                OpenExeConfiguration(exeConfigName);
    
            ConnectionStringsSection section =
                config.GetSection("connectionStrings")
                as ConnectionStringsSection;
    
            if (section.SectionInformation.IsProtected)
            {
                // Remove encryption.
                section.SectionInformation.UnprotectSection();
            }
            else
            {
                // Encrypt the section.
                section.SectionInformation.ProtectSection(
                    "DataProtectionConfigurationProvider");
            }
            // Save the current configuration.
            config.Save();
    
            Console.WriteLine("Protected={0}",
                section.SectionInformation.IsProtected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    
    
    

    Web.config 示例

    此示例使用 WebConfigurationManagerOpenWebConfiguration 方法。注意,在这种情况下,您可以通过使用波形符来提供 Web.config 文件的相对路径。代码需要对 System.Web.Configuration 类的引用。

    static void ToggleWebEncrypt()
    {
        // Open the Web.config file.
        Configuration config = WebConfigurationManager.
            OpenWebConfiguration("~");
    
        // Get the connectionStrings section.
        ConnectionStringsSection section =
            config.GetSection("connectionStrings")
            as ConnectionStringsSection;
    
        // Toggle encryption.
        if (section.SectionInformation.IsProtected)
        {
            section.SectionInformation.UnprotectSection();
        }
        else
        {
            section.SectionInformation.ProtectSection(
                "DataProtectionConfigurationProvider");
        }
    
        // Save changes to the Web.config file.
        config.Save();
    }
    
    
    

    有关保护 ASP.NET 应用程序的更多信息,请参见 ASP.NET 开发中心的 ASP.NET 安全性ASP.NET 2.0 Security Practices at a Glance(ASP.NET 2.0 安全做法简介)。

  • 相关阅读:
    Dell PowerEdge服务器RAID卡驱动下载
    Cent OS yum 安装 Adobe flash player
    如何在安全模式下创建新管理员账户?
    chkdsk 和sfc.exe修复命令
    右下角弹出"Windows-延缓写入失败"或者"xxx-损坏文件 请运行Chkdsk工具"
    VMware NAT模式 Cent OS IP配置
    sublime Text2 2.0.2 build 2221 64位 破解(已测试)
    Windows Server 2008 R2 配置Exchange 2010邮件服务器
    openGL深度缓冲区问题
    glRotatef 转动方向
  • 原文地址:https://www.cnblogs.com/yongbufangqi1988/p/1842128.html
Copyright © 2011-2022 走看看