I also noticed that you included OLEDB. Perhaps the database you were connecting to was really an OLEDB database rather than a MSSQL Server database?
Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Data.OleDb Public Class Form1 Private myC As SqlConnection Private mCm As SqlCommand Private myR As SqlDataReader Private res As String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click myC = New SqlConnection("Initial Catalog = DB; Data Source = serverName; Integrated Security = SSPI;") mCm = myC.CreateCommand mCm.CommandText = "SELECT * FROM table" myC.Open()
Skydiver,
What used to work for VB.NET doesn't work anymore, maybe I need to add a reference.
I tried it for VB.NET and it connected. I tried to do it for C# as well but couldn't get it connected.
I went back to try again for VB.NET the code that works, now it has the same error message as I see in C#.
Could it be that I need to add reference, kind of like VBA need to add Microsoft ActiveX Data Objects Library.
This is what I use for VB.NET
VB.NET:Imports System.IO Imports System.Data Imports System.Data.SqlClient Imports System.Data.OleDb Public Class Form1 Private myC As SqlConnection Private mCm As SqlCommand Private myR As SqlDataReader Private res As String Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click myC = New SqlConnection("Initial Catalog = DB; Data Source = serverName; Integrated Security = SSPI;") mCm = myC.CreateCommand mCm.CommandText = "SELECT * FROM table" myC.Open() myR = mCm.ExecuteReader() End Sub End Class
This is what I use for C#
C#:using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace WFapp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { String connectionstring = null; SqlConnection cnn; connectionstring = "Data Source = serverName; Initial Catalog = Database; Integrated Security = SSPI"; cnn = new SqlConnection(connectionstring); cnn.Open(); } } }
Please Advise !
Try this, add a new item "DataSet" from the "Data" tab when adding a new item to the project.
Add a new TableAdapter and have it generate the connection string to your sql server, once confirming it works (by adding the table) you can copy the connection string (or leave it and just use the My.Settings.<connectionstringname> in your code since it's part of the project) and use it in your code.
You can even delete the DataSet from the project, or learn how to use them which will allow you to query your data using proper code and simpler to maintain since you have the nice visual tool at your disposal.
Well spotted Skydiver, an eye of an Eagle. lol I even overlooked that hidden in VB code. On this note, I think its worth to note the following ::I also noticed that you included OLEDB. Perhaps the database you were connecting to was really an OLEDB database rather than a MSSQL Server database?
The database is actually Microsoft SQL server.
" - So you would be best using an instance based connection string and drop the password. See - .NET Framework Data Provider for SQL Server Connection Strings - ConnectionStrings.comSystem.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
at the entry point of your application.If it's at the firewall level, it should stop VBA as well. If it's an antivirus or nannyware, then they typically run at application level. Your admins may have added the apps that work into their whitelist.
If you don't have admin rights, then yes, uninstall and install will definitely be out of your reach. Repair should be available, though. Go to the Control Panel, find the .NET Framework under Apps (in Win10) or Add/Remove Programs in older Windows, right click, and Repair should be a choice.
Do you not know how to add a new Form to your project? Or a new Class? Or a Module?Juggalobrotha,
Thanks for the advise. I am new to C# and I can’t follow unless with detail or pics, sorry.
I don’t know where dataset and data tab is. Please advise
By adding an app to their white list, add the name of the executable that you are compiling using Visual Studio. If your executable is named "MyAwesomeApp.exe", they need to add "MyAwesomeApp.exe" to their whitelist.
Can Visual Studio still connect to the database?
While I am thinking about it, what connection string are you using for your VBA and R code?
As I recall, for VBA connection strings, you need to also need to have a "Provider=providername" as part of the connection string. That would then imply that you are using on OLEDB connection, not a SQL connection like you are now trying to use with your current VB.NET and C# code. So what provider are you giving as that provider name value?
What driver did you select when testing this connection via Visual Studio?Yes visual studio can connect to the database via tools connect to database and enter server and database name.
OdbcConnection
instead of a SqlConnection
. The connection string for an ODBC connection should look like what you are using for VBA and R.