Aurora 4x

Off Topic => Off Topic => Topic started by: Erik L on October 23, 2007, 04:08:28 PM

Title: Programming
Post by: Erik L on October 23, 2007, 04:08:28 PM
Anyone do anything in .NET 2005?

Got a snippet of code that is supposed to save to a database. It doesn't. I'll be damned if I can figure it out. Written in VB.
Code: [Select]
       'save to the database
        conn = New SqlConnection
        conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\cyberhack.mdf;Integrated Security=True;User Instance=True"

        Dim sCmdText As String = "INSERT INTO Game ([GameName], [GameDate], [GameTime]) VALUES (@GameName, @Date, @Time)"
        Dim cmd As New SqlCommand(sCmdText, conn)
        Dim iRow As Integer

        cmd.Parameters.Add("@GameName", SqlDbType.VarChar, 50)
        cmd.Parameters.Add("@Date", SqlDbType.DateTime)
        cmd.Parameters.Add("@Time", SqlDbType.DateTime)
        cmd.Parameters("@GameName").Value = txtName.Text
        cmd.Parameters("@Date").Value = "01/01/2068"
        cmd.Parameters("@Time").Value = "12:00"

        MsgBox(conn.ConnectionString)

        'cmd.CommandText = "cyberhack.dbo.GameInsert @GameName, @Date, @Time"
        cmd.CommandType = CommandType.Text
        Try
            cmd.Connection = conn
            conn.Open()
            cmd.Prepare()
            iRow = cmd.ExecuteNonQuery
            MsgBox(iRow)
            conn.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


iRow has a value of 1, which means the call returned 1 row like it should. It does not throw any exceptions.
Title:
Post by: TrueZuluwiz on October 23, 2007, 05:52:30 PM
Were you biting your lip while you wrote it? Sometimes that helps.
Title:
Post by: Erik L on October 23, 2007, 06:39:32 PM
I think I was cussing the computer when I was trying to debug it...
Title:
Post by: RoguePhoenix on October 23, 2007, 07:54:32 PM
A nice big rubber mallet is usually helpful. We keep one in our cube for the parts database management server. It usually works pretty well too!

And if we're good lil boys and santa is ever so nice this year we might even be able to office space it when the new system comes in MWUHAHAHHAHA
Title:
Post by: Randy on October 24, 2007, 12:14:29 AM
Not sure if it makes a difference, but I usually have the connection opened before trying to create a comand from it...

ie do
Code: [Select]
conn.open() before
Code: [Select]
Dim cmd As New SqlCommand(sCmdText, conn)

But this assumes that your connect string is working. Are you able to perform a select using that connect string?

I generally keep the connection open (unless doing asp.net) as it works a little faster using connection pooling.

I also tend to assemple the sql string rather than using parameters (they get real annoying sometimes :-)


Then again, I'm only using .net 2003 and Access...
Title:
Post by: Erik L on October 24, 2007, 08:06:33 AM
Quote from: "Randy"
Not sure if it makes a difference, but I usually have the connection opened before trying to create a comand from it...

ie do
Code: [Select]
conn.open() before
Code: [Select]
Dim cmd As New SqlCommand(sCmdText, conn)
But this assumes that your connect string is working. Are you able to perform a select using that connect string?

I generally keep the connection open (unless doing asp.net) as it works a little faster using connection pooling.

I also tend to assemple the sql string rather than using parameters (they get real annoying sometimes :-)


Then again, I'm only using .net 2003 and Access...


Can't say I'd done much data access in 2003. This is a slightly modified version of the example they've got in MSDN. One would think it'd work.

Prior, I had the connection created in the app startup, and destroyed in the app shutdown.

I'd tried the simple SQL string too. That didn't work either. I am starting to think it is a SQL Express issue, but I'll be damned if I know what. The DBA here is confused as well.