?
Solved

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

Posted on 2004-04-23
7
Medium Priority
?
273 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 240 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 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