Home  >  Article  >  Database  >  [.NET] Titan ORM 中执行数据添加Insert-快速示例

[.NET] Titan ORM 中执行数据添加Insert-快速示例

WBOY
WBOYOriginal
2016-06-07 15:07:561081browse

本 示例 使用SqlServer2005,先在 数据 库中创建一张表, 其中CustomerId是主键且自动增长;CustomerType用来保存int枚举,字段类型使用int;InsertTime将系统时间作为默认值 CREATE TABLE [ Customer ] ( [ CustomerId ] [ int ] IDENTITY ( 1 , 1 ) NOT NU

示例使用SqlServer2005,先在数据库中创建一张表,

其中CustomerId是主键且自动增长;CustomerType用来保存int枚举,字段类型使用int;InsertTime将系统时间作为默认值

<span>CREATE</span> <span>TABLE</span> <span>[</span><span>Customer</span><span>]</span><span>(
    </span><span>[</span><span>CustomerId</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>IDENTITY</span>(<span>1</span>,<span>1</span>) <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>CustomerName</span><span>]</span> <span>[</span><span>nvarchar</span><span>]</span>(<span>50</span>) <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>CustomerType</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>Description</span><span>]</span> <span>[</span><span>int</span><span>]</span> <span>NULL</span><span>,
    </span><span>[</span><span>InsertTime</span><span>]</span> <span>[</span><span>datetime</span><span>]</span> <span>NOT</span> <span>NULL</span> <span>default</span>(<span>getdate</span>()), <span>--</span><span>用于测试在<strong>数据</strong>库中设置默认值</span>
    <span>CONSTRAINT</span> <span>[</span><span>PK_Customer</span><span>]</span> <span>PRIMARY</span> <span>KEY</span>(<span>[</span><span>CustomerId</span><span>]</span><span>)
) </span><span>ON</span> <span>[</span><span>PRIMARY</span><span>]</span>

接下来创建相应的实体类,可以使用枚举:

<span>    public</span> <span>enum</span><span> CustomerType
    {
        Home, Abroad
    }

    [Table]
    </span><span>public</span> <span>class</span><span> Customer
    {
        [Column(IsPrimaryKey</span>=<span>true</span><span>)]
        [SqlServerColumn(IsIdentity</span>=<span>true</span>)]<span>//</span><span>针对SqlServer特有的标识列</span>
        <span>public</span> <span>int</span> CustomerId { <span>get</span>; <span>set</span><span>; } 

        [Column]
        </span><span>public</span> <span>string</span> CustomerName { <span>get</span>; <span>set</span><span>; } 

        [Column]
        </span><span>public</span> CustomerType CustomerType { <span>get</span>; <span>set</span><span>; }

        [Column]
        </span><span>public</span> <span>string</span> Description { <span>get</span>; <span>set</span><span>; }

        [Column]
        [SqlServerColumn(GenerateInsert</span>=AttributeBoolean.False)]<span>//<strong>数据</strong>库中已有默认值,</span><span>告诉Titan在生成Insert语句时不要包含本列</span>
        <span>public</span> DateTime InsertTime { <span>get</span>; <span>set</span><span>; } 
    }</span>

使用Titan往数据库中添加一条记录:

<span>    class</span><span> Program
    {
        </span><span>static</span> <span>void</span> Main(<span>string</span><span>[] args)
        {
            IDbSession se </span>=<span> OpenDbSession();

            Customer customer </span>= <span>new</span><span> Customer();
            customer.CustomerName </span>= <span>"</span><span>customer name</span><span>"</span><span>;
            customer.CustomerType </span>=<span> CustomerType.Abroad;

            se.Insert(customer);
            Console.WriteLine(</span><span>string</span>.Format(<span>"</span><span><strong>执行</strong>Insert后标识列返回的CustomerId={0}</span><span>"</span><span>,customer.CustomerId));

            se.Close();

            Console.ReadLine();
        }

        </span><span>static</span><span> IDbSession OpenDbSession()
        {
            </span><span>//</span><span>使用SqlServer。如果是其它<strong>数据</strong>库则可以使用:OracleSqlProvider,MySqlSqlProvider,SQLiteSqlProvider...</span>
            Type providerType = <span>typeof</span><span>(SqlServerSqlProvider);

            </span><span>//</span><span><strong>数据</strong>库连接支付串</span>
            <span>string</span> connectionString = <span>@"</span><span>Data Source=192.168.17.129\SQLEXPRESS;Initial Catalog=titandemo;User Id=sa;Password=123456;</span><span>"</span><span>;

            </span><span>//</span><span>sql语句追踪,可以跟踪Titan生成的Sql语句,此处使用控制台中查看生成的Sql语句</span>
            ISqlTracer[] sqlTracers = <span>new</span> ISqlTracer[] { <span>new</span><span> ConsoleSqlTracer() };

            </span><span>return</span><span> DbSessionFactory.CreateAndOpenSession(providerType, connectionString, sqlTracers); 
        }
    }</span>

查看数据库已经添加成功:

控制台程序运行截屏:

从中可以看到生成的Sql语句中不包含CustomerId列和InsertTime列,

由于CustomerId使用了[SqlServerColumn(IsIdentity=true)]标注,Titan在生成Insert语句时不会包含此列,默认情况下还会在Insert语句中包含set @4=SCOPE_Identity()用以取回数据库自动生成的值。

另外数据库中InsertTime列使用了默认值,并且实体类属性中使用了[SqlServerColumn(GenerateInsert=AttributeBoolean.False)]标注,因此生成的Sql语句中也不包含此列。 

关于IsIdentity=true标注,如果在[Column]标注中强制不返回,那么Insert语句中set @4=SCOPE_Identity()语句不会被生成。代码如下(注意红色部分):

<span>    [Table]
    </span><span>public</span> <span>class</span><span> Customer
    {
        [Column(IsPrimaryKey </span>= <span>true</span>, <span>ReturnAfterInsert =</span><span><span> AttributeBoolean.False</span>)]
        [SqlServerColumn(IsIdentity </span>= <span>true</span>)]<span>//</span><span>针对SqlServer特有的标识列</span>
        <span>public</span> <span>int</span> CustomerId { <span>get</span>; <span>set</span>; }

再运行程序,发现Insert语句中不再包含set @4=SCOPE_Identity(),CustomerId的值仍为0。

 

 

 

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn