C
C#2y ago
Davaaron

❔ Get an entity with Sql data client

Hi, Im trying to read some data with the sql data client and Im stuck. The data looks like this: ID (uniqueidentifer, PK), Forename (nvarchar), Lastname (nvarchar), Birthday (datetime) The code looks like this
// Set the connection, command, and then execute the command with query and return the reader.
public static SqlOutput<DataTable> ExecuteReader(String connectionString, String commandText,
CommandType commandType = CommandType.Text, params SqlParameter[] parameters)
{
return Execute(cmd =>
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read()) // HERE THE ERROR IS HAPPENING
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
}
return new DataTable();
}, commandType, parameters);

}


var result = SqlHelper.ExecuteReader(ConnectionString, input.script, System.Data.CommandType.Text, input.parameters.Select(x => // input.parameters = @[{'E48CC209-4D6A-4594-BD02-AE74DBCF82EA'}]
{
var p = new SqlParameter(x.Key, System.Data.SqlDbType.UniqueIdentifier);
p.Value = Guid.Parse(x.Value);
return p;
}).ToArray());
// Set the connection, command, and then execute the command with query and return the reader.
public static SqlOutput<DataTable> ExecuteReader(String connectionString, String commandText,
CommandType commandType = CommandType.Text, params SqlParameter[] parameters)
{
return Execute(cmd =>
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read()) // HERE THE ERROR IS HAPPENING
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
}
return new DataTable();
}, commandType, parameters);

}


var result = SqlHelper.ExecuteReader(ConnectionString, input.script, System.Data.CommandType.Text, input.parameters.Select(x => // input.parameters = @[{'E48CC209-4D6A-4594-BD02-AE74DBCF82EA'}]
{
var p = new SqlParameter(x.Key, System.Data.SqlDbType.UniqueIdentifier);
p.Value = Guid.Parse(x.Value);
return p;
}).ToArray());
The error message is:
System.Data.SqlClient.SqlException: "Error converting a character in "uniqueidentifer"."
System.Data.SqlClient.SqlException: "Error converting a character in "uniqueidentifer"."
Somebody has an idea what's wrong? That the script for the table
2 Replies
Davaaron
Davaaron2y ago
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'User')
BEGIN
CREATE TABLE [dbo].[User]
(
Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT NEWID(),
LastName VARCHAR(50) NOT NULL,
ForeName VARCHAR(50) NOT NULL,
Birthday datetime NOT NULL
);
PRINT 1;
END
ELSE PRINT 0
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'User')
BEGIN
CREATE TABLE [dbo].[User]
(
Id UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT NEWID(),
LastName VARCHAR(50) NOT NULL,
ForeName VARCHAR(50) NOT NULL,
Birthday datetime NOT NULL
);
PRINT 1;
END
ELSE PRINT 0
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.