Solved

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

Posted on 2004-04-23
7
260 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

20 Experts available now in Live!

Get 1:1 Help Now