日期:2014-05-18 浏览次数:20506 次
public void AddLeaveword(String sTitle,String sBody) { //定义数据库的Connection and Command SqlConnection myConnection = new SqlConnection(SQLCONNECTIONSTRING); SqlCommand myCommand = new SqlCommand("Pr_AddLeaveword",myConnection); //定义访问数据库的方式为存储过程 myCommand.CommandType = CommandType.StoredProcedure; //创建访问数据库的参数 SqlParameter parameterTitle = new SqlParameter("@title",SqlDbType.VarChar,30); parameterTitle.Value = sTitle; myCommand.Parameters.Add(parameterTitle); SqlParameter parameterBody = new SqlParameter("@messageContent",SqlDbType.VarChar,400); parameterBody.Value = sBody; myCommand.Parameters.Add(parameterBody); //SqlParameter parameterleaveName = new SqlParameter("@leaveName", SqlDbType.VarChar, 20); //parameterleaveName.Direction = ParameterDirection.ReturnValue; //myCommand.Parameters.Add(parameterleaveName); //SqlConnection conn = new SqlConnection("data source=(local);uid=sa;pwd=;database=data;pooling=true"); 这四句被我注释掉了,本来书上是这么给leaveName赋值的,可是这里我的leaveName为空。。因为同学的登陆界面还没做好 不想编Session测试,想这先用插入看下可以不。。 SqlConnection conn = new SqlConnection("data source=(local);uid=sa;pwd=;database=data;pooling=true"); String strSQL = "insert into leaveMessage(userID,leaveName)" + "values(admin , bluyzkl111)"; SqlCommand comm = new SqlCommand(strSQL, conn);//插入userID和leaveName try { //打开数据库的连接 myConnection.Open(); } catch(Exception ex) { throw new Exception("数据库连接失败!",ex); } try { //执行数据库的存储过程(访问数据库) myCommand.ExecuteNonQuery(); } catch(Exception ex) { throw new Exception(ex.Message,ex); } finally { if (myConnection.State == ConnectionState.Open) { //关闭数据库的连接 myConnection.Close(); } } //return (char)parameterleaveName.Value;这句被我注释掉了,然后函数类型被我改成空的。。 }
SqlParameter parameterleaveName = new SqlParameter("@leaveName", SqlDbType.VarChar, 20); parameterleaveName.Direction = ParameterDirection.ReturnValue; parameterleaveName.Value="test"+DateTime.ToString("yyyyMMddhhmmss"); myCommand.Parameters.Add(parameterleaveName);
------解决方案--------------------
jf