• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Tony Qu
我的软件工作室
博客园    首页    新随笔    联系   管理     

如何在sqlcommand中使用默认值

我在microsoft newsgroup发的原文:
In Sql Server, I can use syntax to insert a record with default value:
insert table1(a,b,c)values('A','B',default)

But in .Net, if I want to use default value in Adapter.InsertCommand,for
example,

SqlAdapObj.InsertCommand.Parameters.Add(New
SqlClient.SqlParameter("@testField"))
SqlAdapObj.InsertCommand.Parameters("@testField").Value=default

There is no "default" in C# language, so it says error occurred,what const
variable should I use to do so

相关回复:

*************************************************************************
ADO.NET does not know how to handle DEFAULT syntax on its own, but you can
handle this issue by creating a custom SqlCommand that uses the DEFAULT
keyword to load the default value defined for a column. In this case you
would not define the third column (as you have done)--so you only need the
first two parameters defined.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.

**************************************************************************
To support default values you need to add custom code to the table mapper
(<TableName>Collection) class. Usually you will copy the standard insert
method from the <TableName>Collection_Base class and slightly modify it to
support defaults.


Let's say the following standard Insert method and the 'Country' field has a default value:


public virtual void Insert(UserRow value)
{
    string sqlStr = "INSERT INTO [dbo].[User] (" +
        "[ID], " +
        "[Name], " +
        "[Country]" +
        ") VALUES (" +
        _db.CreateSqlParameterName("ID") + ", " +
        _db.CreateSqlParameterName("Name") + ", " +
        _db.CreateSqlParameterName("Country") + ")";
    IDbCommand cmd = _db.CreateCommand(sqlStr);
    AddParameter(cmd, "ID", value.ID);
    AddParameter(cmd, "Name", value.Name);
    AddParameter(cmd, "Country", value.Country);
    cmd.ExecuteNonQuery();
}


To create a method that inserts data with the default Contry value, perform the following steps:
- Copy the Insert methods from <TableName>Collection_Base to <TableName>Collection
- Rename the new method to InsertWithDefaultCountry
- Remove all 'Country' related code from the new method


public virtual void InsertWithDefaultCountry(UserRow value)
{
    string sqlStr = "INSERT INTO [dbo].[User] (" +
        "[ID], " +
        "[Name] " +
        ") VALUES (" +
        _db.CreateSqlParameterName("ID") + ", " +
        _db.CreateSqlParameterName("Name") + ")";
    IDbCommand cmd = _db.CreateCommand(sqlStr);
    AddParameter(cmd, "ID", value.ID);
    AddParameter(cmd, "Name", value.Name);
    cmd.ExecuteNonQuery();
}


The code above inserts a new record with the default 'Country' value, however it does not update the Country property in the value object. If you want to update the value object, you can utilize the RapTier generated GetByPrimaryKey method.


public virtual void InsertWithDefaultCountry(UserRow value)
{
    string sqlStr = "INSERT INTO [dbo].[User] (" +
        "[ID], " +
        "[Name] " +
        ") VALUES (" +
        _db.CreateSqlParameterName("ID") + ", " +
        _db.CreateSqlParameterName("Name") + ")";
    IDbCommand cmd = _db.CreateCommand(sqlStr);
    AddParameter(cmd, "ID", value.ID);
    AddParameter(cmd, "Name", value.Name);
    cmd.ExecuteNonQuery();

    // Update the Country property
    UserRow newValue = GetByPrimaryKey(value.ID);
    value.Country = newValue.Country;
}

so the conclusion is that you have to skip the column name in a parameter if you want it to be updated with default values

--
Vijay Sachan
MCP - ASP.NET


版权声明:本文由作者Tony Qu原创, 未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则视为侵权。
posted @ 2005-03-18 07:33  找事的狐狸  阅读(1019)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3