Adding a row

R

Roy Gourgi

Hi,

I am trying to add a row to my database. I first made a data connection with
Database Explorer where I have made the connection to my database royDBnew
and my table tblRoy with 2 columns SOBN, BN1 (both int). Then I made a data
source connection in the Data Sources. When I run the program I do not get
any errors, but it does not seem to add the row into my table. When I check
in the Database Explorer in my tblRoy it has not added the row. What am I
doing wrong. Below is my code.

TIA
Roy


using System;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using System.Text;

namespace testing

{

class Class1

{

[STAThread]

static void Main(string[] args)

{

int lnSOBN = 900;

int lnBN1 = 188;

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\royDBnew.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

SqlConnection conn = new SqlConnection(strConnection);

string strInsert = "INSERT INTO [tblRoy ] (SOBN, BN1) VALUES (@par0, @par1)
";


SqlCommand cmd = new SqlCommand(strInsert, conn);

SqlParameter parameter1 = new SqlParameter("@par0", SqlDbType.Int, 255);

SqlParameter parameter2 = new SqlParameter("@par1", SqlDbType.Int, 255);

parameter1.Value = lnSOBN;

parameter2.Value = lnBN1;

cmd.Parameters.Add(parameter1);

cmd.Parameters.Add(parameter2);

cmd.Connection.Open();

cmd.ExecuteNonQuery();

cmd.Connection.Close();

}

}

}



Thanks

Roy
 
J

John Richardson

maybe your int size? but I don't see how this would be the problem cause I
would figure an error would bounce back if it was a problem. Maybe someone
else sees something more obvious. I think an int is sized as 4, although I
have put in 0 without issues, too.

Unless SqlExpress does things differently. I'm not familiar with this, nor
how AttachDbFilename works within a conn string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top