Kreij
Senior Monkey Moderator
- Joined
- Feb 6, 2007
- Messages
- 13,817 (2.07/day)
- Location
- Cheeseland (Wisconsin, USA)
For any of you who roll you own CRUD parameters for SQL Server database table access, this will save you a ton of time. The code below will query the table you give it and automatically generate the SqlParameters for your commands. It parses the table and gets the column name, type and size.
First, my SqlUtilities class ...
To utilize this static class from your code (this one uses a stored procedure) ...
If you have an autonumbering identity column that you want updated on an insert just add this after the "AddRange" line above ...
Add the command(s) to the SqlDataAdapter after you create them...
(Remember you must have a new instance of SqlParameters for each command)
Delete commands are usually only a couple of lines, so I do not user the auto-parameter creation code for them.
Sorry for not color coding everything. It's a pain in the lower posterior region, and I am writing an offline BBCode editor as you read this to make my life easier when posting this stuff.
I have left out a lot of details to keep this from being a novel, so if anyone has any questions feel free to post.
Oh, and by the way, you may have noticed that the namespace (and project name) is "Revelation".
That's the project I use to test new, unproven stuff. Seems a fitting name.
Happy coding !!
First, my SqlUtilities class ...
Code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
// using System.Windows.Forms; // Uncomment this if you want to use a MessageBox
namespace Revelation
{
public static class SqlUtilities
{
private static SqlConnection connection = new SqlConnection(Properties.Settings.Default.DBConnection);
private static List<KeyValuePair<string, SqlDbType>> dbTypeList = new List<KeyValuePair<string, SqlDbType>> {
new KeyValuePair<string, SqlDbType>("int", SqlDbType.Int),
new KeyValuePair<string, SqlDbType>("varchar", SqlDbType.VarChar),
new KeyValuePair<string, SqlDbType>("bit", SqlDbType.Bit),
new KeyValuePair<string, SqlDbType>("datetime", SqlDbType.DateTime),
new KeyValuePair<string, SqlDbType>("decimal", SqlDbType.Decimal)
// Add any or all of the SqlDbTypes into this list.
};
private static SqlDbType FindInDbTypeList(string NativeDbType)
{
foreach (KeyValuePair<string, SqlDbType> _DbListItem in dbTypeList)
{
if (_DbListItem.Key == NativeDbType) return _DbListItem.Value;
}
return SqlDbType.Variant; // If all the possible choices are in the List, this is not good.
}
private static DataSet GetColumsAndTypes(string Table)
{
DataSet _DataSet;
SqlCommand _Command = new SqlCommand("SELECT syscolumns.name, syscolumns.length, systypes.name FROM syscolumns " +
"INNER JOIN systypes ON syscolumns.xtype = systypes.xtype " +
"WHERE syscolumns.id = object_id('" + Table + "')", connection);
SqlDataAdapter _Adapter = new SqlDataAdapter(_Command);
_Adapter.Fill(_DataSet = new DataSet(), Table);
return _DataSet;
}
public static SqlParameter[] GetParameters(string Table)
{
SqlParameter[] _Parameters = new SqlParameter[0];
DataSet _DataSet = GetColumsAndTypes(Table);
foreach (DataRow _Row in _DataSet.Tables[0].Rows)
{
Array.Resize(ref _Parameters, _Parameters.Length + 1);
_Parameters[_Parameters.Length - 1] = new SqlParameter("@" + _Row[0].ToString(), FindInDbTypeList(_Row[2].ToString()),
Convert.ToInt32(_Row[1]), _Row[0].ToString());
}
return _Parameters;
}
}
}
To utilize this static class from your code (this one uses a stored procedure) ...
Code:
SqlCommand _Command = new SqlCommand("my_StoredProcedure", connectionString);
_Command.CommandType = CommandType.StoredProcedure;
SqlParameter _Parameters = SqlUtilities.GetParameters("Your table name");
_Command.Parameters.AddRange(_Parameters);
If you have an autonumbering identity column that you want updated on an insert just add this after the "AddRange" line above ...
Code:
_Command.Parameters["@YourColumnName"].Direction = ParameterDirection.Output;
Add the command(s) to the SqlDataAdapter after you create them...
(Remember you must have a new instance of SqlParameters for each command)
Code:
_myAdapter.InsertCommand = _Command;
Delete commands are usually only a couple of lines, so I do not user the auto-parameter creation code for them.
Sorry for not color coding everything. It's a pain in the lower posterior region, and I am writing an offline BBCode editor as you read this to make my life easier when posting this stuff.
I have left out a lot of details to keep this from being a novel, so if anyone has any questions feel free to post.
Oh, and by the way, you may have noticed that the namespace (and project name) is "Revelation".
That's the project I use to test new, unproven stuff. Seems a fitting name.

Happy coding !!

Last edited: