如何仅将一个IDENTITY列插入表中(SQL Express) [英] How to insert into a table with just one IDENTITY column (SQL Express)

查看:105
本文介绍了如何仅将一个IDENTITY列插入表中(SQL Express)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题.

但是我似乎无法使它与Visual Studio 2008中的SQL Server Express一起使用.

But I can't seem to get this to work with SQL Server Express in Visual Studio 2008.

相同的表格布局,一列带有标识和主键.

Same Table layout, One column with identity and primary key.

推荐答案

 INSERT INTO dbo.TableWithOnlyIdentity DEFAULT VALUES

这对我来说很好.您如何尝试将这些行放入数据库中? SQL Server Mgmt Studio?从.NET应用程序进行SQL查询?

This works just fine in my case. How are you trying to get those rows into the database? SQL Server Mgmt Studio? SQL query from .NET app?

在Visual Studio的新建查询"窗口中运行,我得到:

Running inside Visual Studio in the "New Query" window, I get:

DEFAULT VALUES SQL构造或 不支持该语句.

The DEFAULT VALUES SQL construct or statement is not supported.

==>好的,因此Visual Studio无法处理它-这不是SQL Server的问题,而是Visual Studio的问题.而是使用真正的SQL Management Studio-在这里可以正常工作!

==> OK, so Visual Studio can't handle it - that's not the fault of SQL Server, but of Visual Studio. Use the real SQL Management Studio instead - it works just fine there!

使用ADO.NET的过程也很有趣:

Using ADO.NET also works like a charm:

using(SqlConnection _con = new SqlConnection("server=(local);
                             database=test;integrated security=SSPI;"))
{
    using(SqlCommand _cmd = new SqlCommand
            ("INSERT INTO dbo.TableWithOnlyIdentity DEFAULT VALUES", _con))
    {
        _con.Open();
        _cmd.ExecuteNonQuery();
        _con.Close();
    }
}   

似乎是VS的局限性-不要在大型​​数据库工作中使用VS :-) 马克

Seems to be a limitation of VS - don't use VS for serious DB work :-) Marc

这篇关于如何仅将一个IDENTITY列插入表中(SQL Express)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆