我想在我的文本框中获取max(EmployeeID)+1“txt_EmployeeID” [英] I want get max (EmployeeID )+1 in my textbox "txt_EmployeeID"
问题描述
我的存储过程是
创建过程[dbo] .SP @ SelectMaxID
AS
BEGIN
DECLARE @IN INT
SELECT @ IN = mAX(EmployeeID)+ 1 FROM dbo.Employee
SELECT @IN AS D
END
和源代码
con = new SqlConnection(connectionstring);
SqlCommand hari = new SqlCommand();
SqlDataReader dr =(null);
cmd = new SqlCommand(SP @ SelectMaxID,con);
cmd。 CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ EMPID,SqlDbType.NVarChar,30).Value = txt_EmployeeID.Text;
con.Open( );
txt_EmployeeID.Text = dr [EmployeeID]。ToString();
< b><% - 这里我不能把正确的对象 - % < br $> b $ b
//cmd.ExecuteNonQuery();
cmd.ExecuteReader ();
con.Close();
< b>我想在我的文本框中获取max(EmployeeID)+1txt_EmployeeID
对存储过程进行少许更改
创建 proc [dbo] .SP @ SelectMaxID
AS
BEGIN
DECLARE @ IN INT
SELECT @ IN = mAX(ISNULL(EmployeeID, 0 ))+ 1 FROM dbo.Employee
SELECT @ IN AS EmployeeID
END
,代码中的变化也很少
con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand( SP @SelectMaxID,con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
string id = cmd.ExecuteScalar();
con.Close();
txt_EmployeeID.Text = id;
希望,它有帮助:)
如果您的问题仍未解决,请与我们联系。
首先你的存储过程应该是这样的:
create proc [dbo] .SP @ SelectMaxID
@ IN INT 输出
AS
BEGIN
SELECT @ IN = mAX(EmployeeID) + 1 FROM dbo.Employee
SELECT @ IN AS D
END
您的源代码应如下所示:
string RESULT =
DataSet DT = new DataSet();
SqlDataAdapter DA;
SqlCommand cmd = new SqlCommand();
Object results = null;
cmd.CommandText =SP @ SelectMaxID;
cmd.CommandTimeout = 4800;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = ConString;
尝试
{
cmd.Connection.Open();
cmd.Parameters.AddWithValue(@ IN,RESULT);
cmd.ExecuteNonQuery();
DA = new SqlDataAdapter(cmd);
DA.Fill(DT);
txt_EmployeeID.Text = DT.Tables [0] .Rows [0] [D];
}
最后
{
if(cmd.Connection.State> 0)
{
cmd.Connection.Close();
}
}
您可以调用您的程序,例如
con = new SqlConnection(connectionstring);
SqlCommand hari = new SqlCommand();
cmd = new SqlCommand( SP @ SelectMaxID,con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
var maxID = cmd.ExecuteScalar();
con.Close();
将maxID放在文本框中:
txt_EmployeeID.Text = maxId.ToString( )
my stored procedures is
create proc [dbo].SP@SelectMaxID
AS
BEGIN
DECLARE @IN INT
SELECT @IN= mAX(EmployeeID) + 1 FROM dbo.Employee
SELECT @IN AS D
END
and Source code
con = new SqlConnection(connectionstring);
SqlCommand hari = new SqlCommand();
SqlDataReader dr = (null);
cmd = new SqlCommand("SP@SelectMaxID", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@EMPID", SqlDbType.NVarChar, 30).Value = txt_EmployeeID.Text;
con.Open();
txt_EmployeeID.Text = dr["EmployeeID"].ToString();
<%-- here i can't put the right object --%
//cmd.ExecuteNonQuery();
cmd.ExecuteReader();
con.Close();
I want get max(EmployeeID) +1 in my textbox "txt_EmployeeID"
Make little changes in your stored proc
create proc [dbo].SP@SelectMaxID AS BEGIN DECLARE @IN INT SELECT @IN= mAX(ISNULL(EmployeeID,0)) + 1 FROM dbo.Employee SELECT @IN AS EmployeeID END
and few changes in the code too
con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand("SP@SelectMaxID", con); cmd.CommandType = CommandType.StoredProcedure; con.Open(); string id=cmd.ExecuteScalar(); con.Close(); txt_EmployeeID.Text =id;
Hope, it helps :)
Please let me know if your problem is still not resolved.
Firstly Your Stored Proc should look like this:
create proc [dbo].SP@SelectMaxID @IN INT OUTPUT AS BEGIN SELECT @IN= mAX(EmployeeID) + 1 FROM dbo.Employee SELECT @IN AS D END
Your Source Code should look something like this:
string RESULT = "" DataSet DT = new DataSet(); SqlDataAdapter DA; SqlCommand cmd = new SqlCommand(); Object results = null; cmd.CommandText = "SP@SelectMaxID"; cmd.CommandTimeout = 4800; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = ConString; try { cmd.Connection.Open(); cmd.Parameters.AddWithValue("@IN", RESULT); cmd.ExecuteNonQuery(); DA = new SqlDataAdapter(cmd); DA.Fill(DT); txt_EmployeeID.Text = DT.Tables[0].Rows[0]["D"]; } finally { if (cmd.Connection.State > 0) { cmd.Connection.Close(); } }
You can call your procedure like
con = new SqlConnection(connectionstring); SqlCommand hari = new SqlCommand(); cmd = new SqlCommand("SP@SelectMaxID", con); cmd.CommandType = CommandType.StoredProcedure; con.Open(); var maxID=cmd.ExecuteScalar(); con.Close();
The put the maxID in your text box:
txt_EmployeeID.Text=maxId.ToString()
这篇关于我想在我的文本框中获取max(EmployeeID)+1“txt_EmployeeID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!