我正在使用 VS 2008 Asp.net C#
。要插入我创建的数据stored procedure
:
ALTER PROCEDURE INS_tblRegister
@Enroll varchar,
@Name varchar,
@Dob varchar,
@Gender varchar,
@Mobile varchar,
@Address varchar,
@Branch varchar,
@Sem varchar,
@Year varchar,
@Password varchar
AS
INSERT INTO tblRegister (Enrollment, Name, Dob, Gender, Mobile, Address, Branch, Sem, Year, Password)
VALUES (@Enroll,@Name,@Dob,@Gender,@Mobile,@Address,@Branch,@Sem,@Year,@Password)
RETURN
以下是button_Click
的代码:
try
{
con.Open();
cmd = new SqlCommand("INS_tblRegister",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Enroll", SqlDbType.VarChar).Value = txtEnroll.Text;
cmd.Parameters.Add("Name", SqlDbType.VarChar).Value = txtName.Text;
cmd.Parameters.Add("Dob", SqlDbType.VarChar).Value = (txtDob.Text);
cmd.Parameters.Add("Gender", SqlDbType.VarChar).Value = ddlGender.Text;
cmd.Parameters.Add("Mobile", SqlDbType.VarChar).Value = txtMobile.Text;
cmd.Parameters.Add("Address", SqlDbType.VarChar).Value = txtAddress.Text;
cmd.Parameters.Add("Branch", SqlDbType.VarChar).Value = txtBranch.Text;
cmd.Parameters.Add("Sem", SqlDbType.VarChar).Value = txtSem.Text;
cmd.Parameters.Add("Year", SqlDbType.VarChar).Value = txtYear.Text;
cmd.Parameters.Add("Password", SqlDbType.VarChar).Value = txtPassword.Text;
cmd.ExecuteNonQuery();
con.Close();
lblMsg.Text="Saved";
}
catch(Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
此代码已成功执行,但当我看到数据库时,我发现:
只保存了我的条目的第一个字符,例如:查看性别字段,我写了男性,但只有M插入数据库。请帮帮我......
答案 0 :(得分:2)
答案 1 :(得分:1)
将SP的变量声明为@Enroll varchar(size)
,其中size为int值。实施例@Enroll varchar(500)
。 varchar只会考虑第一个字符,因为它的长度是1。
答案 2 :(得分:1)
您需要为存储过程参数添加字符长度,如下所示:
ALTER PROCEDURE INS_tblRegister
@Enroll varchar(20),@Name varchar(20),@Dob varchar(20),@Gender varchar(20),@Mobile varchar(20),@Address varchar(20), @Branch varchar(20), @Sem varchar(20), @Year varchar(20), @Password varchar(20)
AS
INSERT INTO tblRegister (Enrollment, Name, Dob, Gender, Mobile, Address, Branch, Sem, Year, Password)
VALUES (@Enroll,@Name,@Dob,@Gender,@Mobile,@Address,@Branch,@Sem,@Year,@Password)
RETURN
答案 3 :(得分:0)
提供每个varchar字段的长度。
ALTER PROCEDURE INS_tblRegister
@Enroll varchar(10),@Name varchar(50),
@Dob varchar(10),@Gender varchar(6),@Mobile varchar(10),@Address varchar(100),
等...
答案 4 :(得分:0)
您需要提及 varchar 的大小。试试这个程序。
ALTER PROCEDURE INS_tblRegister
@Enroll varchar(50),@Name varchar(50), @Dob varchar(50), @Gender varchar(50), @Mobile varchar(50), @Address varchar(50),
@Branch varchar(50), @Sem varchar(50), @Year varchar(50), @Password varchar(50)
AS
INSERT INTO tblRegister(Enrollment, Name, Dob, Gender, Mobile, Address, Branch, Sem, Year, Password) VALUES(@Enroll,@Name,@Dob,@Gender,@Mobile,@Address,@Branch,@Sem,@Year,@Password)
RETURN
默认varchar
大小为1个字符。
答案 5 :(得分:0)
varchar[(n)]
如果未在数据定义或变量声明语句中指定n,则默认长度为1.
ALTER PROCEDURE INS_tblRegister
@Enroll varchar(20),@Name varchar(20),@Dob varchar(20),@Gender varchar(20),@Mobile varchar(20),@Address varchar(20), @Branch varchar(20), @Sem varchar(20), @Year varchar(20), @Password varchar(20)
AS
INSERT INTO tblRegister (Enrollment, Name, Dob, Gender, Mobile, Address, Branch, Sem, Year, Password)
VALUES (@Enroll,@Name,@Dob,@Gender,@Mobile,@Address,@Branch,@Sem,@Year,@Password)
RETURN