I appreciate any help, I've been working on this for a while.
Thank you,
- Steve Harclerode
-------------- code 1 begin (WORKS!)
string connectionString = "server=" + server + "; uid=" + userId +
"; pwd=" + password + ";";
System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection( connectionString );
con.Open();
System.Data.SqlClient.SqlCommand cmd =
new System.Data.SqlClient.SqlCommand();
cmd.Connection = con;
cmd.CommandText = "InsertHealthData @xmlText='" + xmlText + "'";
cmd.ExecuteNonQuery();
con.Close();
-------------- code 1 end
-------------- code 2 begin (FAILS)
string connectionString = "server=" + server + "; uid=" + userId +
"; pwd=" + password + ";";
System.Data.SqlClient.SqlConnection con =
new System.Data.SqlClient.SqlConnection( connectionString );
con.Open();
System.Data.SqlClient.SqlCommand cmd =
new System.Data.SqlClient.SqlCommand();
cmd.Connection = con;
cmd.CommandText = "InsertHealthData";
System.Data.SqlClient.SqlParameter param =
cmd.Parameters.Add("@xmlText", SqlDbType.Text );
/* this doesn't work either: cmd.Parameters.Add("@xmlText", SqlDbType.Text,
2147483647 ) */
param.Direction = ParameterDirection.Input;
param.Value = xmlText;
cmd.ExecuteNonQuery();
con.Close();
-------------- code 2 end
-------------- exception begin
System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near
'InsertHealthData'.
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
-------------- exception end
You are not including your parameter in the CommandText. I think it would
work if you set it like this:
cmd.CommandText = "InsertHealthData xmlText=@xmlText";
I have added some sample code that might be of help.
-Juan Gabriel
//////////
/// Inserting a row into a table named Users
///
To insert a row into a table I would do this:
cmd.CommandText = "INSERT INTO Users(UserName, FullName, Age) " +
"VALUES(@name, @fullName, @description)";
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = name;
cmd.Parameters.Add("@fullName", SqlDbType.VarChar, 100).Value = fullName;
cmd.Parameters.Add("@age", SqlDbType.Int).Value = age;
And to call a stored procedure that looks like this:
CREATE STORED PROCUDURE Users_Insert
@name varchar(20),
@fullName varchar(100),
@age int
AS ...
I would do this:
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Users_Insert @name, @fullName, @age";
cmd.Parameters.Add("@name", SqlDbType.VarChar, 20).Value = name;
cmd.Parameters.Add("@fullName", SqlDbType.VarChar, 100).Value = fullName;
cmd.Parameters.Add("@age", SqlDbType.Int).Value = age;
However, I also had to change the CommandText to "Exec InsertHealthData"
before the name of the stored proc, in order to get it to work with the
explicit parameter.
I'm going to write Jesse Liberty about this -- I used an example from his
ASP.NET book (which is otherwise quite good, in my opinion).
-- Steve Harclerode
"Juan Gabriel Del Cid" <jde...@atrevido.nospam.net> wrote in message
news:eEVPjFB7...@TK2MSFTNGP12.phx.gbl...
- Steve
"Steve Harclerode" <CamelSoftw...@hotmail.com> wrote in message
news:#l7BAuB7...@TK2MSFTNGP11.phx.gbl...
-JG
"Steve Harclerode" <CamelSoftw...@hotmail.com> wrote in message
news:er3he0B7...@TK2MSFTNGP12.phx.gbl...
Thanks again.
-j
------
Jesse Liberty, President
Liberty Associates, Inc.
"Steve Harclerode" <CamelSoftw...@hotmail.com> wrote in message
news:er3he0B7...@TK2MSFTNGP12.phx.gbl...
- Steve
"Jesse Liberty" <jlib...@libertyassociates.com> wrote in message
news:#O9jEIN7...@TK2MSFTNGP11.phx.gbl...