form connection to SQL server

JohnSe

Active member
Joined
Jun 17, 2019
Messages
27
Programming Experience
5-10
Hi,

Could someone provide some codes examples that can help me connect C# forms to SQL server that is trusted (without user name or password, just the server and database name).

Thanks
 
Did you mean VB.NET?
.NET is a framework on which various languages are built on like: C#, VB.NET, F#, IronPython, etc.

Post the connection string you use in VB.NET as well as the connection string you use in C#.
 
I'm not in a habit of repeating myself, so I will ask for the last time. What is the connection string you are currently using?

Please post it along with the screenshot of your SQL services management section? Just as I have done above.

The error in which you are getting is screaming the problem right at you, and in 90% of the topics I answer on forums regarding this issue, its always user error providing the wrong details to their connection strings. Also read this to troubleshoot your issue since you're not exactly forthcoming with the requested info there's little more we can do to help you, if you don't provide the info I asked for. This will also help you if you rather go at it alone. https://social.technet.microsoft.co...ecting-to-the-sql-server-database-engine.aspx
 
Did you mean VB.NET?
.NET is a framework on which various languages are built on like: C#, VB.NET, F#, IronPython, etc.

Post the connection string you use in VB.NET as well as the connection string you use in C#.


Yes it is VB.NET. In addition to VB.NET connection, I can connect with various other languages such as R as well.
I think I only have a Server Name but not also an Instance Name.

With VB.NET connection -
VB.NET:
           mC = New SqlConnection("Initial Catalog = dbName; Data Source = Server; Integrated Security = SSPI;")
           Cmd = mC.CreateCommand
           Cmd.CommandText = "SELECT * FROM table"
           mC.Open()
           myReader = Cmd.ExecuteReader()

With C# connection -
I tried various ways and this is one of them. It always stuck on sqlda.Fill(dtbl);
Please advise !

C#:
         SqlConnection sqlCon = new SqlConnection(@"Data Source = Server;Initial Catalog= dbName;Integrated Security = True;");
         SqlDataAdapter sqlda = new SqlDataAdapter("SELECT * FROM table", sqlCon);
         DataTable dtbl = New DataTable();
         sqlda.Fill(dtbl);
         Foreach(DataRow row in dtbl.Rows)
         {
                       Console.WriteLine(row["col1"]);
         }
         Console.ReadKey();
 
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
What you posted is for ce. Not trusted as you claimed to use.
 
Last edited:
The exact connection string you used for VB.NET should work for C#. You don't need to change anything because both VB.NET and C# both use the same SqlConnection class.

I implore you, please post the exact connection string you are using instead of the fake strings you keep showing us.
 
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
What you posted is for ce. Not trusted as you climed to use.
What is ce ? That is the exact connection string. I only changed the server address and database name. I don’t know why it works for VB.net but not C#.
 
The exact connection string you used for VB.NET should work for C#. You don't need to change anything because both VB.NET and C# both use the same SqlConnection class.

I implore you, please post the exact connection string you are using instead of the fake strings you keep showing us.

Let me trying doing what I have done in VB.NET to C# again and see if that works and get back to you. I thought I have tried that many times but it connected for VB.NET but never C#. It always has stop at cx.open() for C#. Don’t know why. cx.open() is an example I use for C#.
 
This is what you told us you needed to use :: Trusted Connection - SqlConnection - ConnectionStrings.com

But your connection string is for a CE device :: Trusted Connection from a CE device - SqlConnection - ConnectionStrings.com

I am confused for you, since you don't seem to know which one you need. Are you connecting to a CE device or an Instance server or SQL Express? Because it matters.

I say trusted connection because it doesn’t need username and password. Your second example needs username and password, without it that would be the same exact string I am using in my VB.NET codes that connected.
 
@JohnSe My second example can still be used if you use the correct connection string and change every instance from MySQL to SQL, and that's as simple as removing the word My from MySQL. If you don't want a password, then don't supply one.

The exact connection string you used for VB.NET should work for C#. You don't need to change anything because both VB.NET and C# both use the same SqlConnection class.
That is correct. The language makes no difference. If its not working, you've done something different in your C# code than it is in your VB.Net code.
I implore you, please post the exact connection string you are using instead of the fake strings you keep showing us.
Now there's a thought.

Have you followed this posts instructions yet? https://social.technet.microsoft.co...ecting-to-the-sql-server-database-engine.aspx I posted it for you earlier on and It guides you though the process of troubleshooting
 
The exact connection string you used for VB.NET should work for C#. You don't need to change anything because both VB.NET and C# both use the same SqlConnection class.

I implore you, please post the exact connection string you are using instead of the fake strings you keep showing us.

I have tried many different combinations of what I did with VB.Net to C# and it didn’t work. I will show exactly what I did with both but alias server and database name and see what we can trouble shoot ?
 
don’t know why, what used to connect in VB.net but now doesn’t with the same string connection and code. I get the same error I do with C#. I don’t know why.

But I am able to connect with other languages just not vb or C#. Could do that with vb before. Does anyone know why, could it be something that was done on the network or server side that caused dot net framework to unable to connect when it was able to connect before ?
 
Last edited:
Hang on there till I find a genie in a bottle who can tell me what your tried...

You never showed us what you recently tried by placing both language codes side by side with the connection strings visible and unchanged as already requested.
 
Hang on there till I find a genie in a bottle who can tell me what your tried...

You never showed us what you recently tried by placing both language codes side by side with the connection strings visible and unchanged as already requested.

Alright I’ll post something but it won’t be much different than what I have already posted. I was able to connect with VBA. Maybe I need to go to project and add the correct reference in visual studio like I have done to VBA ?
 
The exact connection string you used for VB.NET should work for C#. You don't need to change anything because both VB.NET and C# both use the same SqlConnection class.

I implore you, please post the exact connection string you are using instead of the fake strings you keep showing us.

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 !
 
Last edited:
Back
Top Bottom