首页上一页 1 下一页尾页 1 条记录 1/1页
C#开发宝典中关于房屋中介管理系统 调用SQL存储过程中问题
发表在C#图书答疑
2010-12-13
是否精华
是
否
版块置顶:
是
否
在SQL写了
create proc Login_insert
@employee_ID varchar (10)=null,
@employee_name varchar (20)=null,
@employee_PassWord varchar(20)
as
begin
select @employee_ID=Max(ID) from Login
if(@employee_ID is null)
set @employee_ID='User1001'--作动见编号就知道是什么表
else
set @employee_ID='User'+cast(cast(substring(@employee_ID,4,4) as int)+1 as varchar(20))
insert into Login values(@employee_ID,@employee_name,@employee_PassWord,@employee_ql)
end
在C#中建 buttion
private void BtnAdd_Click(object sender, EventArgs e)
{
Proc_User ce = new Proc_User();
ce.id = this.TxtID.Text.Trim().ToString();
ce.name = this.TxtName.Text.Trim().ToString();
ce.PassWord = this.TxtPassword.Text.Trim().ToString();
ce.ql = this.TxtQL.Text.Trim().ToString();
try
{
Login_insert cem = new Login_insert();
if (cem.Insert_Table(ce))
{
MessageBox.Show("OK");
//this.Close();
}
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
在Proc_User代码如下:
string User_ID;
string User_name;
string User_PassWord;
string User_ql;
public string id
{
get { return User_ID; }
set { User_ID = value; }
}
public string name
{
get { return User_name; }
set { User_name = value; }
}
public string PassWord
{
get { return User_PassWord; }
set { User_PassWord = value; }
}
public string ql
{
get { return User_ql; }
set { User_ql = value; }
}
类Login_insert代码如下
DBConn con = new DBConn();
public bool Insert_Table(Proc_User cf)
{
try
{
con.ConDataBase();
SqlCommand cmd = new SqlCommand("Login_Insert", con.conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection.Open();
SqlParameter[] prams =
{
new SqlParameter("@User_ID", SqlDbType.VarChar, 10),
new SqlParameter("@User_name", SqlDbType.VarChar, 20),
new SqlParameter("@User_PassWord", SqlDbType.VarChar, 20)
};
prams[0].Value = cf.id;
prams[1].Value = cf.name;
prams[2].Value = cf.PassWord;
// 添加参数
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
con.closeCon();
return true;
}
catch
{
con.closeCon();
return false;
}
为什么我插入数据不成功,也不显示在那儿出错了!
把SQL2005中存储过程这一段代码删除 了就行
select @employee_ID=Max(ID) from Login
if(@employee_ID is null)
set @employee_ID='User1001'--作动见编号就知道是什么表
else
set @employee_ID='User'+cast(cast(substring(@employee_ID,4,4) as int)+1 as varchar(20))
请问我那儿出错了,少了什么?
create proc Login_insert
@employee_ID varchar (10)=null,
@employee_name varchar (20)=null,
@employee_PassWord varchar(20)
as
begin
select @employee_ID=Max(ID) from Login
if(@employee_ID is null)
set @employee_ID='User1001'--作动见编号就知道是什么表
else
set @employee_ID='User'+cast(cast(substring(@employee_ID,4,4) as int)+1 as varchar(20))
insert into Login values(@employee_ID,@employee_name,@employee_PassWord,@employee_ql)
end
在C#中建 buttion
private void BtnAdd_Click(object sender, EventArgs e)
{
Proc_User ce = new Proc_User();
ce.id = this.TxtID.Text.Trim().ToString();
ce.name = this.TxtName.Text.Trim().ToString();
ce.PassWord = this.TxtPassword.Text.Trim().ToString();
ce.ql = this.TxtQL.Text.Trim().ToString();
try
{
Login_insert cem = new Login_insert();
if (cem.Insert_Table(ce))
{
MessageBox.Show("OK");
//this.Close();
}
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
在Proc_User代码如下:
string User_ID;
string User_name;
string User_PassWord;
string User_ql;
public string id
{
get { return User_ID; }
set { User_ID = value; }
}
public string name
{
get { return User_name; }
set { User_name = value; }
}
public string PassWord
{
get { return User_PassWord; }
set { User_PassWord = value; }
}
public string ql
{
get { return User_ql; }
set { User_ql = value; }
}
类Login_insert代码如下
DBConn con = new DBConn();
public bool Insert_Table(Proc_User cf)
{
try
{
con.ConDataBase();
SqlCommand cmd = new SqlCommand("Login_Insert", con.conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection.Open();
SqlParameter[] prams =
{
new SqlParameter("@User_ID", SqlDbType.VarChar, 10),
new SqlParameter("@User_name", SqlDbType.VarChar, 20),
new SqlParameter("@User_PassWord", SqlDbType.VarChar, 20)
};
prams[0].Value = cf.id;
prams[1].Value = cf.name;
prams[2].Value = cf.PassWord;
// 添加参数
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
con.closeCon();
return true;
}
catch
{
con.closeCon();
return false;
}
为什么我插入数据不成功,也不显示在那儿出错了!
把SQL2005中存储过程这一段代码删除 了就行
select @employee_ID=Max(ID) from Login
if(@employee_ID is null)
set @employee_ID='User1001'--作动见编号就知道是什么表
else
set @employee_ID='User'+cast(cast(substring(@employee_ID,4,4) as int)+1 as varchar(20))
请问我那儿出错了,少了什么?