Solved

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

Posted on 2004-04-23
7
263 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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