使用数据源将简单的dbTable添加到我的项目后,我在VS 2019中总是收到错误消息。我使用MS SQL,为此,我使用Microsoft SQL Server Management Studio。我无法创建connectString,现在无法生成数据源。有人知道为什么吗?
CREATE TABLE [dbo].[Table] ([Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) )
我不确定您怎么了。
但是,我可以告诉您实现连接字符串的详细步骤。
首先,您可以使用以下sql语句在Microsoft SQL Server Management Studio中创建表。
第二,返回Visual Studio 2019->选择工具->连接到数据库->选择Microsoft SQL Server。
第三,将服务器名称从SQL Server Management Studio复制到当前表单。
然后,选择表存在的数据库,单击“高级”,您将发现底行将有一个文本框。这是连接字符串。
最后,将其复制并使用以下代码对其进行测试。
private void button1_Click(object sender, EventArgs e)
{
try
{
string connectstring = "Data Source=Servername;Initial Catalog=test2;Integrated Security=True";
SqlConnection connection = new SqlConnection(connectstring);
connection.Open();
MessageBox.Show("success");
}
catch
{
MessageBox.Show("Failed");
}
}