G
Greif
I am creating some methods for disconected data
maniuplations. I have came up with the following method to
insert a single row into the DB:
public virtual int singleInsert(string tableName, params
IDbDataParameter[] values)
{
if(ConnectionString == "" ||
ConnectionString.Length == 0)
throw new InvalidOperationException("The
connection string is not set. Set the 'ConnectionString'
member.");
string strInsert = "INSERT into " + tableName + "
(%%<columns>%%) VALUES(%%<values>%%)";
System.Text.StringBuilder strColumns = new
System.Text.StringBuilder();
foreach(IDbDataParameter param in values)
{
strColumns.Append("@" +
param.ParameterName + ",");
}
//Remove the trailing ','
strColumns.Remove(strColumns.Length -1, 1);
strInsert = strInsert.Replace("%%<values>%
%",strColumns.ToString());
strInsert = strInsert.Replace("%%<columns>%
%",strColumns.ToString().Replace("@",""));
return ExecuteNonQuery(CommandType.Text,
strInsert, values);
}
Is this an acceptable way to insert data? Am I missing
something pretty basic? It seems to me there is a better
way to do this but I haven't found it (writer's block I
guess hehe).
Any help would be greatly appreciated.
maniuplations. I have came up with the following method to
insert a single row into the DB:
public virtual int singleInsert(string tableName, params
IDbDataParameter[] values)
{
if(ConnectionString == "" ||
ConnectionString.Length == 0)
throw new InvalidOperationException("The
connection string is not set. Set the 'ConnectionString'
member.");
string strInsert = "INSERT into " + tableName + "
(%%<columns>%%) VALUES(%%<values>%%)";
System.Text.StringBuilder strColumns = new
System.Text.StringBuilder();
foreach(IDbDataParameter param in values)
{
strColumns.Append("@" +
param.ParameterName + ",");
}
//Remove the trailing ','
strColumns.Remove(strColumns.Length -1, 1);
strInsert = strInsert.Replace("%%<values>%
%",strColumns.ToString());
strInsert = strInsert.Replace("%%<columns>%
%",strColumns.ToString().Replace("@",""));
return ExecuteNonQuery(CommandType.Text,
strInsert, values);
}
Is this an acceptable way to insert data? Am I missing
something pretty basic? It seems to me there is a better
way to do this but I haven't found it (writer's block I
guess hehe).
Any help would be greatly appreciated.