problem with stored procedure
-
I have 1 table student(ID,Name,Address). and i make a stored procedure
CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END
so i can't get the MaxID in C#. Please help me ! sorry for my english. -
I have 1 table student(ID,Name,Address). and i make a stored procedure
CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END
so i can't get the MaxID in C#. Please help me ! sorry for my english.Why can't you ? What have you tried ? ExecuteScalar may work, if not, just build a data reader and read your field.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
I have 1 table student(ID,Name,Address). and i make a stored procedure
CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END
so i can't get the MaxID in C#. Please help me ! sorry for my english.Why would you want to?
-
Why can't you ? What have you tried ? ExecuteScalar may work, if not, just build a data reader and read your field.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Why would you want to?
-
So, if I get this right, what you're saying is that you don't know anything about C# at all and have done nothing to try to learn it ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
So, if I get this right, what you're saying is that you don't know anything about C# at all and have done nothing to try to learn it ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Sorry, i have learned C# for 2 weeks :)
SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID));
With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank ! -
I have 1 table student(ID,Name,Address). and i make a stored procedure
CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END
so i can't get the MaxID in C#. Please help me ! sorry for my english.CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] ( @MaxID int OUTPUT ) as if exists(SELECT [ID] FROM student) SELECT @MaxID = MAX(ID) FROM student else SET @MaxID=1 ---------------------------- Because '@MaxID' is OUTPUT So Your stored procedure... :) ---------------------------- Chinese programmers: 狼人软件
-
Sorry, i have learned C# for 2 weeks :)
SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID));
With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank !SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
-
Sorry, i have learned C# for 2 weeks :)
SqlConnection connect = new SqlConnection(ConnectionFactory.strconn); connect.Open(); cmd = new SqlCommand("Proc_student_Add", connect); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("MaxID",varScenario.ID));
With that stored procedured i can't get return value by "int i = cmd.ExecuteNonQuery();", please help me to resolve this problem. Thank !If you've been learning C# for two weeks, why are you working on calling stored procedures ? There's no way that's a sensible next step for you at this point. The MSDN site is a good place to look for documentation. You should really also own at least one C# book. Either way, ExecuteNonQuery returns a number that tells you how many rows were changed. The documentation will tell you this. I told you in the first instance that you should call ExecuteScalar, if you had any idea what you were doing, or willingness to do a little research, you should have been able to work that out. Even the intellisense would show you the answer. If you're doing a course, you should talk to your teacher about getting some resources to help you do some basic research. If, as I expect, you're doing paid work, then you're yet another example of the disgraceful excuse for an industry that has popped up in the third world, where people with no experience work on paid systems and then expect the people in the West who lose their jobs to you, to do the work for you. There is no level on which someone who started to learn C# two weeks ago, should be working on this sort of code, unless they have sufficient experience in another language to be able to do some basic research for themselves and to understand a post like my initial reply.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
That's a whole lot more work than is necessary, but I accept it would work.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
SqlCommand sampleCMD = new SqlCommand("SampleProc", nwindConn); sampleCMD.CommandType = CommandType.StoredProcedure; SqlParameter sampParm = sampleCMD.Parameters.Add("RETURN_VALUE", SqlDbType.Int); sampParm.Direction = ParameterDirection.ReturnValue; sampParm = sampleCMD.Parameters.Add("@InputParm", SqlDbType.NVarChar, 12); sampParm.Value = "Sample Value"; sampParm = sampleCMD.Parameters.Add("@OutputParm", SqlDbType.NVarChar, 28); sampParm.Direction = ParameterDirection.Output; nwindConn.Open(); SqlDataReader sampReader = sampleCMD.ExecuteReader(); Console.WriteLine("{0}, {1}", sampReader.GetName(0), sampReader.GetName(1)); while (sampReader.Read()) { Console.WriteLine("{0}, {1}", sampReader.GetInt32(0), sampReader.GetString(1)); } sampReader.Close(); nwindConn.Close(); Console.WriteLine(" @OutputParm: {0}", sampleCMD.Parameters["@OutputParm"].Value); Console.WriteLine("RETURN_VALUE: {0}", sampleCMD.Parameters["RETURN_VALUE"].Value);
-
I have 1 table student(ID,Name,Address). and i make a stored procedure
CREATE PROCEDURE [dbo].[Proc_Scenario_GetMaxID] @MaxID as int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) end else Begin SET @MaxID=1 END return @MaxID END
so i can't get the MaxID in C#. Please help me ! sorry for my english.REATE PROCEDURE Proc_Scenario_GetMaxID @MaxID int OUTPUT as BEGIN if((SELECT MAX(ID) FROM student) is not null) begin SET @MaxID = (SELECT MAX(ID) FROM student) return 0 end else Begin SET @MaxID=1 return 1 END end code for execution :-- declare @i int exec Proc_Scenario_GetMaxID @i output print convert(char(4),@i))
-
For what purpose?