Solved

Cannot establish a Connection from Vb.NET to SQL Server. None of the exapmles is running!

Posted on 2004-04-23
7
261 Views
Last Modified: 2010-04-24
Hallo everyone,

I have succesfully programmed (got the code from someone else) a windows service in .NET.
So far this service is running a timer and executing whatever I want after a specific period.

I am an oldschool ASP scripter. My functions are all written in VBScript on ASP Webpages. Now suddenly I have to realize a monitoring service that can react on specific Logfile information in textfiles and that can generate (INSERT!) some other data on the same db. So I realized that my service calls an invisible Internet Explorer window that opens my old ASP Page. The ASP looks into the LOG File and can react by inserting data to SQL Server.

It works, but i FEEL that is NOT the best solution...  :)

So I want my service to opne the text file, evaluate the data and wrinte into SQL Server.

My problem: I cannot get ANY .NET Application open a connection to SQL Server and read, add or manipulate some data. I have tried several examples from lots of different Webpages and forums but it never runs.
I use Framework 1.1 and VB Studio 2003.

Can anyone please give my some running code that INSERTS data into my SQL Database - for 80 Points, all that is left  ;)

--------------------------------------------------------
Here one of the examples NOT running:  
(I have tested the Db Connection not in my service project but always with a new Win Application Project)

I get the following error in the mySQLConnection.open() - line.

A not handeled Exception from type 'System.Data.SqlClient.SqlException' appeared in system.data.dll.
Additional Information: Systemerror.


'-----------------------------------------
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Vom Windows Form Designer generierter Code "
'(...)
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim myDataReader As SqlDataReader
        Dim mySqlConnection As SqlConnection
        Dim mySqlCommand As SqlCommand
        mySqlConnection = New SqlConnection("server=(local)\VSdotNET;uid=test;pwd=test;database=northwind")
        mySqlCommand = New SqlCommand("SELECT EmployeeID, LastName, FirstName, Title, ReportsTo FROM Employees", mySqlConnection)
        mySqlConnection.Open()
        myDataReader = mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
        Do While (myDataReader.Read())
            Console.Write(myDataReader.GetInt32(0).ToString() + Chr(9))
            Console.Write(myDataReader.GetString(2) + " " + myDataReader.GetString(1) + Chr(9))
            Console.Write(myDataReader.GetString(3) + Chr(9))
            If (myDataReader.IsDBNull(4)) Then
                Console.Write("N/A" + Chr(10))
            Else
                Console.Write(myDataReader.GetInt32(4).ToString() + Chr(10))
            End If
        Loop
        myDataReader.Close()
        mySqlConnection.Close()
    End Sub
End Class
0
Comment
Question by:MPKR
7 Comments
 
LVL 4

Accepted Solution

by:
MajinLoki earned 80 total points
ID: 10899694
mySqlConnection = New SqlConnection("server=(local)\VSdotNET;uid=test;pwd=test;database=northwind")

I'm not sure this is correct syntax.  Try this instead

mySqlConnection = New SqlConnection("server=localhost;uid=test;pwd=test;database=northwind")

Let me know if that works.

Majin Loki
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10900050
This is my connection string for MSDE,maybe such format will work for you:

"Server=alex_comp\VSDOTNET;DataBase=northwind;Integrated Security=SSPI;"
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10900128
hmmm...let me show you a sample connection that I have used.

server=localhost;trusted_connection=true;database=eCRM;

where eCRM is the name of my database.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:MajinLoki
ID: 10900145
In my experiece using htis SQL statement, the program will use the database using the users Windows Login as the user.  So, you have to set the user up to be able to do what you want them to do on the SQL server.  if you allow public to read from your database, then you will be fine for adding, inserting and reading.  Deleteing requires special permisssion, unless that user is an administrator on the database.
0
 
LVL 4

Expert Comment

by:Jarodtweiss
ID: 10900203
When you have a doubt on a connection string, check it here !
http://www.connectionstrings.com
0
 
LVL 4

Author Comment

by:MPKR
ID: 10900272
OK so far that has helped me!!!!!

I am getting data now, I did not see the Instance
"(server=(local)\VSdotNET;)"
after the (local), I don't have this instance...! :)

Ok, now I want to INSERT insetead of Select Data, can anyone give the example (I think DataReader is not the right one eh?)

Thanks!
0
 
LVL 4

Expert Comment

by:MajinLoki
ID: 10900399
mySqlCommand = New SqlCommand("INSERT INTO tablename VALUES('col1','col2')",mySqlConnection)
mySqlCommand.ExecuteNonQuery()

if the column is a string or date, put single quites around it.  if it is null or a number, do not put quotes around it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now