C
cwineman
Hello,
I'm using SQL Server and I have a column for storing photos of the database
type "image". I want it to be optionally NULL. I have a stored procedure
that I use to populate the table. When I run the stored procedure from the
query analyzer tool, I have no problems. I can pass in a NULL parameter for
this column, or pass in actual content.
From .NET I have a problem setting the parameter correctly. I can pass in
actual content. In this case the byte [] buffer for an image file. However,
when I try to pass in DBNull.Value, I get the error "nvarchar is
incompatible with image". I don't understand what the problem is.
Has anyone else had this error? Has anyone else stored null values in a
column of type image through .NET?
I've posted some code below to give you and idea of what I am doing.
Thanks,
Corey
_connectionString = "Data Source=DBMGR;" + "Initial Catalog=MyDatabase;" +
"User ID = MyID;" + "Password = MyPassword;";
_connection = (IDbConnection)Activator.CreateInstance(
typeof(SqlConnection), false);
_connection.ConnectionString = _connectionString;
_command = (IDbCommand)Activator.CreateInstance( typeof(SqlCommand), false);
_command.Connection = _connection;
_command.CommandType = System.Data.CommandType.StoredProcedure;
Image img = Image.FromFile("C:\\temp\\a.jpg");
MemoryStream ms = new MemoryStream();
img.Save( ms, System.Drawing.Imaging.ImageFormat.Jpeg );
byte [] myBytes = new byte[4];
myBytes[0] = 1;
myBytes[1] = 2;
myBytes[2] = 3;
myBytes[3] = 4;
string paramName = "@my_column";
object val = myBytes;// null;//(object) ms.ToArray();
ParameterDirection direction = ParameterDirection.Input;
int size = 50;
_parameter = (IDbDataParameter)Activator.CreateInstance(
typeof(SqlParameter), false );
_parameter.ParameterName = paramName;
if(val!=null)
{
_parameter.Value = val;
}
else
{
_parameter.Value = DBNull.Value;
_parameter.Size = size;
}
_parameter.Direction = direction;
_command.Parameters.Clear();
_command.CommandText = "SBASE_DALSP_Insert_Test";
_command.Parameters.Add( _parameter );
_connection.Open();
_command.ExecuteNonQuery();
I'm using SQL Server and I have a column for storing photos of the database
type "image". I want it to be optionally NULL. I have a stored procedure
that I use to populate the table. When I run the stored procedure from the
query analyzer tool, I have no problems. I can pass in a NULL parameter for
this column, or pass in actual content.
From .NET I have a problem setting the parameter correctly. I can pass in
actual content. In this case the byte [] buffer for an image file. However,
when I try to pass in DBNull.Value, I get the error "nvarchar is
incompatible with image". I don't understand what the problem is.
Has anyone else had this error? Has anyone else stored null values in a
column of type image through .NET?
I've posted some code below to give you and idea of what I am doing.
Thanks,
Corey
_connectionString = "Data Source=DBMGR;" + "Initial Catalog=MyDatabase;" +
"User ID = MyID;" + "Password = MyPassword;";
_connection = (IDbConnection)Activator.CreateInstance(
typeof(SqlConnection), false);
_connection.ConnectionString = _connectionString;
_command = (IDbCommand)Activator.CreateInstance( typeof(SqlCommand), false);
_command.Connection = _connection;
_command.CommandType = System.Data.CommandType.StoredProcedure;
Image img = Image.FromFile("C:\\temp\\a.jpg");
MemoryStream ms = new MemoryStream();
img.Save( ms, System.Drawing.Imaging.ImageFormat.Jpeg );
byte [] myBytes = new byte[4];
myBytes[0] = 1;
myBytes[1] = 2;
myBytes[2] = 3;
myBytes[3] = 4;
string paramName = "@my_column";
object val = myBytes;// null;//(object) ms.ToArray();
ParameterDirection direction = ParameterDirection.Input;
int size = 50;
_parameter = (IDbDataParameter)Activator.CreateInstance(
typeof(SqlParameter), false );
_parameter.ParameterName = paramName;
if(val!=null)
{
_parameter.Value = val;
}
else
{
_parameter.Value = DBNull.Value;
_parameter.Size = size;
}
_parameter.Direction = direction;
_command.Parameters.Clear();
_command.CommandText = "SBASE_DALSP_Insert_Test";
_command.Parameters.Add( _parameter );
_connection.Open();
_command.ExecuteNonQuery();