1

我有一个 SQL Server 数据库,我刚刚添加了一个新表:-

CREATE TABLE [dbo].[my_table](
    [my_primary_key] [int] NOT NULL,
    [my_data_value] [int] NULL,
PRIMARY KEY CLUSTERED 
(
    [my_primary_key] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

我将新表从 Visual Web Developer 中的数据库资源管理器拖到my_schema.dbml设计窗口中。这为我创建了一个my_schema.vb文件:-

Partial Class my_schemaDataContext

End Class

尽管该表的一些痕迹确实出现在 XML 中:-

<?xml version="1.0" encoding="utf-8"?><Database Name="my_schema" Class="my_schemaDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
  <Connection Mode="AppSettings" ConnectionString="Data Source=MYPC;Initial Catalog=my_schema;Integrated Security=True" SettingsObjectName="MyProgram.My.MySettings" SettingsPropertyName="my_schemaConnectionString" Provider="System.Data.SqlClient" />
  <Table Name="dbo.my_table" Member="my_tables">
    <Type Name="my_table">
      <Column Name="my_primary_key" Type="System.Int32" DbType="Int NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
      <Column Name="my_data_value" Type="System.Int32" DbType="Int" CanBeNull="true" />
    </Type>
  </Table>
</Database>

然后我尝试像这样将行插入表中:-

Try
    db = New my_schemaDataContext("Integrated Security=SSPI; ")
    Dim new_row As my_table
    new_row = New my_table
    new_row.my_primary_key = 1
    new_row.my_data_value = 1
    db.my_tables.InsertOnSubmit(new_row)
    db.SubmitChanges()
Catch ex As Exception
    ' do something
End Try

这会导致异常Invalid Object Name 'dbo.my_table

大概我必须在某处堵塞模式名称,但看不到如何做到这一点。我尝试添加Initial Catalogue=my_schema;到连接字符串,但这导致了异常Keyword Not Supported: Initial Catalogue

有谁知道我还能尝试什么?(我应该注意,我还尝试了相同架构中的其他表,结果相同,我希望这可以消除随机拼写错误导致问题的可能性。)

4

2 回答 2

7

看起来连接字符串不正确。另外我认为Catalogue应该拼写为Catalog.

于 2013-04-19T09:23:42.240 回答
0

DataContext 的构造函数采用连接字符串。

"Integrated Security=SSPI; "

对我来说,这看起来不像是一个有效的连接字符串。

如果您什么都不提供,它将采用创建时设置的连接字符串。这是设置my_schemaConnectionString。所以就这样做:

db = New my_schemaDataContext()

或者,如果您想在运行时更改它:

string connectionString = "whatever";
db = New my_schemaDataContext(connectionString)
于 2013-04-19T09:29:31.733 回答