473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a better way than this?

Hi,

I am adding a row to my table with the code below. It seems to be
inefficient to me as I have to basically build my command with the string
builder. There has to be a better way, no?

What I am trying to do is to search my table for the 2 variables ln1 and
ln2. If they are not found, then one row is added to the table with the
respective values of ln1 and ln2. So for example, if my table contained 100
rows and I perform a search for the values ln1 and ln2 and they are not
found, then one row is added to my table with the values of ln1 and ln2.
Therefore, my table will now have 101 rows.

The code below works, it's just that I think there must be a more efficient
way of doing it.

Another thing that I find that is strange is that when a row is added to my
table, it is added in between rows so that the ascending order is
maintained. Why is that, I as have never asked to sort it or index it.

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

{

// private System.Data.DataSet dataSet;

[STAThread]

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\SQL_ 2\SQL_2.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

int ln1 = 5;

int ln2 = 19;

StringBuilder strCommand = new StringBuilder("INSERT INTO tblSQL_2 (SOBN ,
BN1 ) SELECT ");

strCommand.Append(ln1).Append(" , ").Append(ln2).Append(" WHERE not exists
(select * from tblSQL_2 where SOBN = ");

strCommand.Append(ln1).Append(" AND BN1 = ").Append(ln2).Append(")");

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand.ToString(), oConnection);

oCommand.Connection.Open();

oCommand.ExecuteNonQuery();

oCommand.Connection.Close();

}

}

}
Nov 19 '05 #1
2 1203
Can't you just append things manually to a string object?

T
Roy Gourgi wrote:
Hi,

I am adding a row to my table with the code below. It seems to be
inefficient to me as I have to basically build my command with the string
builder. There has to be a better way, no?

What I am trying to do is to search my table for the 2 variables ln1 and
ln2. If they are not found, then one row is added to the table with the
respective values of ln1 and ln2. So for example, if my table contained 100
rows and I perform a search for the values ln1 and ln2 and they are not
found, then one row is added to my table with the values of ln1 and ln2.
Therefore, my table will now have 101 rows.

The code below works, it's just that I think there must be a more efficient
way of doing it.

Another thing that I find that is strange is that when a row is added to my
table, it is added in between rows so that the ascending order is
maintained. Why is that, I as have never asked to sort it or index it.

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

{

// private System.Data.DataSet dataSet;

[STAThread]

static void Main(string[] args)

{

string strConnection = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=D:\CSRBC\SQL _2\SQL_2.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True";

int ln1 = 5;

int ln2 = 19;

StringBuilder strCommand = new StringBuilder("INSERT INTO tblSQL_2 (SOBN ,
BN1 ) SELECT ");

strCommand.Append(ln1).Append(" , ").Append(ln2).Append(" WHERE not exists
(select * from tblSQL_2 where SOBN = ");

strCommand.Append(ln1).Append(" AND BN1 = ").Append(ln2).Append(")");

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand.ToString(), oConnection);

oCommand.Connection.Open();

oCommand.ExecuteNonQuery();

oCommand.Connection.Close();

}

}

}

Nov 19 '05 #2
tomb wrote:
Can't you just append things manually to a string object?

T


yeah, do it this way:

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;

namespace testing {

class Class1 {

// private System.Data.DataSet dataSet;

[STAThread]
static void Main(string[] args) {

string strConnection = @"Data ";
strConnection += "Source=.\SQLEXPRESS;AttachDbFilename=";
strConnection += "D:\CSRBC\SQL_2\SQL_2.mdf;Integrated "
strConnection +=Security=True;Connect Timeout=30;User Instance=True";

int ln1 = 5;

int ln2 = 19;

string strCommand = "INSERT INTO tblSQL_2 (SOBN , BN1 ) SELECT ";
strCommand += ln1 + " , " + ln2 + " WHERE not exists ";
strCommand += "(select * from tblSQL_2 where SOBN = ";
strCommand += ln1 + " AND BN1 = " + ln2 + ")";

SqlConnection oConnection = new SqlConnection(strConnection);

SqlCommand oCommand = new SqlCommand(strCommand, oConnection);

oCommand.Connection.Open();

oCommand.ExecuteNonQuery();

oCommand.Connection.Close();

}

}

}
Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
10
by: michael newport | last post by:
Dear friends of database(s), After 13 years of Ingres, I am now using Oracle. But is Oracle technically better than Ingres. I would be much obliged if anyone could shed some light on the...
24
by: Faith Dorell | last post by:
I really donīt like C.You can write better programs in BASIC than in C, if you donīt like this language. I donīt understand how C became so popular, although much better programming languages...
43
by: Rob R. Ainscough | last post by:
I realize I'm learning web development and there is a STEEP learning curve, but so far I've had to learn: HTML XML JavaScript ASP.NET using VB.NET ..NET Framework ADO.NET SSL
33
by: Protoman | last post by:
Which is better for general-purpose programming, C or C++? My friend says C++, but I'm not sure. Please enlighten me. Thanks!!!!!
23
by: JoeC | last post by:
I am a self taught programmer and I have figured out most syntax but desigining my programs is a challenge. I realize that there are many ways to design a program but what are some good rules to...
21
by: gavino | last post by:
why?
34
by: pamela fluente | last post by:
I would like to hear your *opinion and advice* on best programming practice under .NET. Given that several time we cannot change: MyCollection.Clear into the instantiation of a NEW...
3
by: Ryan Liu | last post by:
Hi, Is Async I/O (e.g. NetworkStream.Begin/End Read/Write) always better than synchronous I/O? At least as good? When I don't concern about easy or difficult to write code, should I always...
43
by: Pawel_Iks | last post by:
I've read somewhere that c++ is something more than better c ... then I talk with my friend and he claimed that c++ is nothing more than better c ... I tried to explain him that he was wrong but I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.