这是我的源代码:执行成功了,但是ID一栏是空值,要怎么样才能把ID传进去呢??
string strname = txt_name.Text.ToString().Trim();
string strstuid = txt_stuid.Text.ToString().Trim();
string strpwd = txt_pwd.Text.ToString().Trim();
string strsex = ddl_sex.Text.ToString().Trim();
string strbthdate =txt_bthdate.Text.ToString().Trim();
string strmail = txt_mail.Text.ToString().Trim();
string strpostadd = txt_postadd.Text.ToString().Trim();
string strpostcd =txt_postcd.Text.ToString().Trim();
string stregname = txt_egname.Text.ToString().Trim();
string strnative = txt_native.Text.ToString().Trim();
string strnation = txt_nation.Text.ToString().Trim();
string strhmphone = txt_hmphone.Text.ToString().Trim();
string strqq = txt_qq.Text.ToString().Trim();
string strmobile = txt_mobile.Text.ToString().Trim();
string strwkphone = txt_wkphone.Text.ToString().Trim();
string strwkplace = txt_wkplace.Text.ToString().Trim();
string strmajor = ddl_major.Text.ToString().Trim();
int strgrade = Convert.ToInt32(ddl_grade.Text.ToString().Trim());
int strgraduyear = Convert.ToInt32(ddl_graduyear.Text.Trim());
string strgraduschl =ddl_graduschl.Text.ToString().Trim();
string strresume = txt_resume.Text.ToString().Trim();
string strelse = txt_else.Text.ToString().Trim();
//定义接收数组
SqlParameter[] parameters=
{
new SqlParameter("@stuid",SqlDbType.NVarChar,255),
new SqlParameter("@username",SqlDbType.NVarChar,255),
new SqlParameter("@password",SqlDbType.NVarChar,255),
new SqlParameter("@sex",SqlDbType.NVarChar,255),
new SqlParameter("@birthdate",SqlDbType.NVarChar,255),
new SqlParameter("@email",SqlDbType.NVarChar,255),
new SqlParameter("@postaddress",SqlDbType.NVarChar,255),
new SqlParameter("@postcode",SqlDbType.NVarChar,255),
new SqlParameter("@egname",SqlDbType.NVarChar,255),
new SqlParameter("@nativeplace",SqlDbType.NVarChar,255),
new SqlParameter("@nation",SqlDbType.NVarChar,255),
new SqlParameter("@homephone",SqlDbType.NVarChar,255),
new SqlParameter("@QQ",SqlDbType.NVarChar,255),
new SqlParameter("@mobilephone",SqlDbType.NVarChar,255),
new SqlParameter("@workphone",SqlDbType.NVarChar,255),
new SqlParameter("@workplace",SqlDbType.NVarChar,255),
new SqlParameter("@adminssionyear",SqlDbType.Int),
new SqlParameter("@graduyear",SqlDbType.Int),
new SqlParameter("@graduschool",SqlDbType.NVarChar,255),
new SqlParameter("@resume",SqlDbType.NVarChar,255),
new SqlParameter("@note",SqlDbType.NVarChar,255),
new SqlParameter("@gradumajor",SqlDbType.NVarChar,255),
new SqlParameter("@id",SqlDbType.Int)
}; int id = Convert.ToInt32(parameters["@id"].Value);
//传递值
parameters[0].Value=strstuid;
parameters[1].Value = strname;
parameters[2].Value=strpwd;
parameters[3].Value=strsex;
parameters[4].Value=strbthdate;
parameters[5].Value=strmail;
parameters[6].Value=strpostadd;
parameters[7].Value=strpostcd;
parameters[8].Value=stregname;
parameters[9].Value=strnative;
parameters[10].Value = strnation;
parameters[11].Value=strhmphone;
parameters[12].Value=strqq;
parameters[13].Value=strmobile;
parameters[14].Value=strwkphone;
parameters[15].Value=strwkplace;
parameters[16].Value=strgrade;
parameters[17].Value=strgraduyear;
parameters[18].Value=strgraduschl;
parameters[19].Value=strresume;
parameters[20].Value=strelse;
parameters[21].Value=strmajor;
parameters[22].Value = id; SqlDb.RunProcedure("alumni_ADD",parameters);
string strname = txt_name.Text.ToString().Trim();
string strstuid = txt_stuid.Text.ToString().Trim();
string strpwd = txt_pwd.Text.ToString().Trim();
string strsex = ddl_sex.Text.ToString().Trim();
string strbthdate =txt_bthdate.Text.ToString().Trim();
string strmail = txt_mail.Text.ToString().Trim();
string strpostadd = txt_postadd.Text.ToString().Trim();
string strpostcd =txt_postcd.Text.ToString().Trim();
string stregname = txt_egname.Text.ToString().Trim();
string strnative = txt_native.Text.ToString().Trim();
string strnation = txt_nation.Text.ToString().Trim();
string strhmphone = txt_hmphone.Text.ToString().Trim();
string strqq = txt_qq.Text.ToString().Trim();
string strmobile = txt_mobile.Text.ToString().Trim();
string strwkphone = txt_wkphone.Text.ToString().Trim();
string strwkplace = txt_wkplace.Text.ToString().Trim();
string strmajor = ddl_major.Text.ToString().Trim();
int strgrade = Convert.ToInt32(ddl_grade.Text.ToString().Trim());
int strgraduyear = Convert.ToInt32(ddl_graduyear.Text.Trim());
string strgraduschl =ddl_graduschl.Text.ToString().Trim();
string strresume = txt_resume.Text.ToString().Trim();
string strelse = txt_else.Text.ToString().Trim();
//定义接收数组
SqlParameter[] parameters=
{
new SqlParameter("@stuid",SqlDbType.NVarChar,255),
new SqlParameter("@username",SqlDbType.NVarChar,255),
new SqlParameter("@password",SqlDbType.NVarChar,255),
new SqlParameter("@sex",SqlDbType.NVarChar,255),
new SqlParameter("@birthdate",SqlDbType.NVarChar,255),
new SqlParameter("@email",SqlDbType.NVarChar,255),
new SqlParameter("@postaddress",SqlDbType.NVarChar,255),
new SqlParameter("@postcode",SqlDbType.NVarChar,255),
new SqlParameter("@egname",SqlDbType.NVarChar,255),
new SqlParameter("@nativeplace",SqlDbType.NVarChar,255),
new SqlParameter("@nation",SqlDbType.NVarChar,255),
new SqlParameter("@homephone",SqlDbType.NVarChar,255),
new SqlParameter("@QQ",SqlDbType.NVarChar,255),
new SqlParameter("@mobilephone",SqlDbType.NVarChar,255),
new SqlParameter("@workphone",SqlDbType.NVarChar,255),
new SqlParameter("@workplace",SqlDbType.NVarChar,255),
new SqlParameter("@adminssionyear",SqlDbType.Int),
new SqlParameter("@graduyear",SqlDbType.Int),
new SqlParameter("@graduschool",SqlDbType.NVarChar,255),
new SqlParameter("@resume",SqlDbType.NVarChar,255),
new SqlParameter("@note",SqlDbType.NVarChar,255),
new SqlParameter("@gradumajor",SqlDbType.NVarChar,255),
new SqlParameter("@id",SqlDbType.Int)
}; int id = Convert.ToInt32(parameters["@id"].Value);
//传递值
parameters[0].Value=strstuid;
parameters[1].Value = strname;
parameters[2].Value=strpwd;
parameters[3].Value=strsex;
parameters[4].Value=strbthdate;
parameters[5].Value=strmail;
parameters[6].Value=strpostadd;
parameters[7].Value=strpostcd;
parameters[8].Value=stregname;
parameters[9].Value=strnative;
parameters[10].Value = strnation;
parameters[11].Value=strhmphone;
parameters[12].Value=strqq;
parameters[13].Value=strmobile;
parameters[14].Value=strwkphone;
parameters[15].Value=strwkplace;
parameters[16].Value=strgrade;
parameters[17].Value=strgraduyear;
parameters[18].Value=strgraduschl;
parameters[19].Value=strresume;
parameters[20].Value=strelse;
parameters[21].Value=strmajor;
parameters[22].Value = id; SqlDb.RunProcedure("alumni_ADD",parameters);
数据库设置主键ID自增
@ID bigint,
@stuid nvarchar(255),
@username nvarchar(255),
@password nvarchar(50),
@sex nchar(10),
@birthdate nvarchar(50),
@email nvarchar(50),
@postaddress nvarchar(50),
@postcode nchar(10),
@headphoto image,
@egname nchar(10),
@nativeplace nvarchar(50),
@nation nvarchar(50),
@homephone nvarchar(50),
@QQ nvarchar(50),
@mobilephone nvarchar(50),
@workphone nvarchar(50),
@workplace nvarchar(50),
@myclass nvarchar(50),
@adminssionyear smallint,
@graduyear smallint,
@graduschool nvarchar(50),
@resume nvarchar(255),
@note nchar(255),
@gradumajor nvarchar(255) AS
INSERT INTO [alumni](
[ID],[stuid],[username],[password],[sex],[birthdate],[email],[postaddress],[postcode],[headphoto],[egname],[nativeplace],[nation],[homephone],[QQ],[mobilephone],[workphone],[workplace],[myclass],[adminssionyear],[graduyear],[graduschool],[resume],[note],[gradumajor]
)VALUES(
@ID,@stuid,@username,@password,@sex,@birthdate,@email,@postaddress,@postcode,@headphoto,@egname,@nativeplace,@nation,@homephone,@QQ,@mobilephone,@workphone,@workplace,@myclass,@adminssionyear,@graduyear,@graduschool,@resume,@note,@gradumajor
)
这是调用的runprocedure方法: public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters )
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataReader returnReader;
connection.Open();
SqlCommand command = BuildQueryCommand( connection,storedProcName, parameters );
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader();
return returnReader;
}
问题在这
你没有指定id
ALTER PROCEDURE dbo.alumni_ADD
@stuid nvarchar(255),
@username nvarchar(255),
@password nvarchar(50),
@sex nchar(10),
@birthdate nvarchar(50),
@email nvarchar(50),
@postaddress nvarchar(50),
@postcode nchar(10),
@headphoto image,
@egname nchar(10),
@nativeplace nvarchar(50),
@nation nvarchar(50),
@homephone nvarchar(50),
@QQ nvarchar(50),
@mobilephone nvarchar(50),
@workphone nvarchar(50),
@workplace nvarchar(50),
@myclass nvarchar(50),
@adminssionyear smallint,
@graduyear smallint,
@graduschool nvarchar(50),
@resume nvarchar(255),
@note nchar(255),
@gradumajor nvarchar(255) AS
INSERT INTO [alumni](
[stuid],[username],[password],[sex],[birthdate],[email],[postaddress],[postcode],[headphoto],[egname],[nativeplace],[nation],[homephone],[QQ],[mobilephone],[workphone],[workplace],[myclass],[adminssionyear],[graduyear],[graduschool],[resume],[note],[gradumajor]
)VALUES(
@stuid,@username,@password,@sex,@birthdate,@email,@postaddress,@postcode,@headphoto,@egname,@nativeplace,@nation,@homephone,@QQ,@mobilephone,@workphone,@workplace,@myclass,@adminssionyear,@graduyear,@graduschool,@resume,@note,@gradumajor
)
改为自动增长后,你以后添加数据时是不用给出ID的值的,你仔细看看吧