?
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
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

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