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
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