C# 使用Visual Studio 2015 Windows窗体应用程序建立与SQL Server的连接
我正在尝试创建一个连接到SQL Server数据库的窗口窗体应用程序。我能够使用Alteryx、Tableau、Python和VisualStudio中的连接向导连接到数据库。但是,当我尝试在应用程序中创建连接时,它无法连接。我尝试了多种方法来实现这一点C# 使用Visual Studio 2015 Windows窗体应用程序建立与SQL Server的连接,c#,sql-server,vb.net,database-connection,C#,Sql Server,Vb.net,Database Connection,我正在尝试创建一个连接到SQL Server数据库的窗口窗体应用程序。我能够使用Alteryx、Tableau、Python和VisualStudio中的连接向导连接到数据库。但是,当我尝试在应用程序中创建连接时,它无法连接。我尝试了多种方法来实现这一点 VB C#使用SqlClient C#使用配置文件(带有测试连接的详细信息) C#使用ODBC驱动程序 然而,当我运行这段代码时,我得到了以下错误 激活的事件持续时间线程异常:引发异常: System.Data.dll中的“System.Dat
/******** Python using Jupiter Notebook ********/
import pyodbc
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=********;'
'Database=********;'
'Uid=********;'
'Pwd=********;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM [********].[dbo].[********]')
for row in cursor:
print(row)
如果我可以通过所有其他方式连接,为什么我不能通过Windows窗体应用程序连接。我假设我所有的TCP/IP设置都正常?另外,我已经通过Visual Studio建立了连接。我将
ODBCConnection
更改为SQLConnection
。这对我很有效,但我决不是专家。我在VB.NET中工作得更好,所以这段代码被转换成C#using。我还在底部发布了VB.NET代码,以防在转换过程中出错
C#代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
TestConnection();
}
private void TestConnection()
{
string connectionString = null;
SqlConnection cnn;
SqlConnectionStringBuilder cnBuild = new SqlConnectionStringBuilder();
cnBuild.DataSource = "Server";
cnBuild.InitialCatalog = "databaseName";
cnBuild.UserID = "userid";
cnBuild.Password = "password";
// connectionString = "Driver={SQL Server Native Client 11.0};Server=********;Database=********;Uid=********;Pwd=********;"
cnn = new SqlConnection(cnBuild.ConnectionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception __unusedException1__)
{
MessageBox.Show("Can not open connection ! ");
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Imports System
Imports System.Windows.Forms
Imports System.Data.SqlClient
Namespace WindowsFormsApplication2
Partial Public Class Form1
Inherits Form
Public Sub New()
TestConnection()
End Sub
Private Sub TestConnection()
Dim connectionString As String = Nothing
Dim cnn As SqlConnection
Dim cnBuild As New SqlConnectionStringBuilder
cnBuild.DataSource = "Server"
cnBuild.InitialCatalog = "databaseName"
cnBuild.UserID = "userid"
cnBuild.Password = "password"
cnn = New SqlConnection(cnBuild.ConnectionString)
Try
cnn.Open()
MessageBox.Show("Connection Open ! ")
cnn.Close()
Catch __unusedException1__ As Exception
MessageBox.Show("Can not open connection ! ")
End Try
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
End Namespace
VB.Net代码
using System;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
TestConnection();
}
private void TestConnection()
{
string connectionString = null;
SqlConnection cnn;
SqlConnectionStringBuilder cnBuild = new SqlConnectionStringBuilder();
cnBuild.DataSource = "Server";
cnBuild.InitialCatalog = "databaseName";
cnBuild.UserID = "userid";
cnBuild.Password = "password";
// connectionString = "Driver={SQL Server Native Client 11.0};Server=********;Database=********;Uid=********;Pwd=********;"
cnn = new SqlConnection(cnBuild.ConnectionString);
try
{
cnn.Open();
MessageBox.Show("Connection Open ! ");
cnn.Close();
}
catch (Exception __unusedException1__)
{
MessageBox.Show("Can not open connection ! ");
}
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Imports System
Imports System.Windows.Forms
Imports System.Data.SqlClient
Namespace WindowsFormsApplication2
Partial Public Class Form1
Inherits Form
Public Sub New()
TestConnection()
End Sub
Private Sub TestConnection()
Dim connectionString As String = Nothing
Dim cnn As SqlConnection
Dim cnBuild As New SqlConnectionStringBuilder
cnBuild.DataSource = "Server"
cnBuild.InitialCatalog = "databaseName"
cnBuild.UserID = "userid"
cnBuild.Password = "password"
cnn = New SqlConnection(cnBuild.ConnectionString)
Try
cnn.Open()
MessageBox.Show("Connection Open ! ")
cnn.Close()
Catch __unusedException1__ As Exception
MessageBox.Show("Can not open connection ! ")
End Try
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
End Namespace
可能重复的Ummm,我已经通过了许多类似的帖子,并且已经检查过我使用的是默认实例,可以接受除外部连接之外的其他连接。我无法检查TCP/IP设置,因为我没有正确的权限,但由于我已成功连接到Python,我假设这些都是确定的,框架中还有一个类可能对@Mary有所帮助-谢谢,我已经使用了这两个资源,还没能破解它。我还观看了一段youtube视频,该视频为连接创建了一个单独的类,但也不起作用。除非有人能看到我遗漏的上述代码有问题,否则我想知道是否有特定于Windows窗体(vs Visual Studio应用程序)的内容,包括特定的防火墙/其他权限。我真的迷路了!!明天我会尝试一下,一旦我回到办公室-ThanksHi@DjJazzyJeffTN-C代码有一些bug,所以我尝试了VB。不确定它是否连接。15秒后它没有超时,但确实发送了一条消息说它已连接。我需要搞乱代码,让它打开一个按钮点击检查连接,但没有机会阿特米是害怕与C#转换器。我很高兴VB代码为您工作。既然你说它行得通,那么你介意把这个作为答案吗?因为问题似乎是连接字符串不正确。谢谢,编码很愉快。我需要测试它是否真的建立了连接。抱歉,我现在正忙于其他事情。等我有空的时候,我会回到这个问题上来,把它改好。感谢您是否测试过我与您共享的代码?