private bool insertLog(string serviceID)
{
SqlConnection myConnection = new SqlConnection(condb.getConnStr());
SqlCommand myCommand = new SqlCommand("instertlog", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter WorkParm = null;
WorkParm = myCommand.Parameters.Add("@tel", SqlDbType.Char,15);
WorkParm = myCommand.Parameters.Add("@serviceId", SqlDbType.Char,20);
WorkParm = myCommand.Parameters.Add("@returnCode", SqlDbType.VarChar ,50);
WorkParm = myCommand.Parameters.Add("@ErrContent", SqlDbType.VarChar,128);
WorkParm = myCommand.Parameters.Add("@operTime", SqlDbType.DateTime,8);
myCommand.Parameters ["@tel"].Value = feeUserID.Text.Trim();
myCommand.Parameters ["@serviceId"].Value = serviceID;
myCommand.Parameters ["@returnCode"].Value = retCode;//retCode
myCommand.Parameters ["@ErrContent"].Value = retStr;
myCommand.Parameters ["@operTime"].Value = DateTime.Now.GetDateTimeFormats();
int Iret = 0;
try
{
myConnection.Open();
Iret = myCommand.ExecuteNonQuery() ;
}
finally
{
myConnection.Dispose();
myConnection.Close();
myCommand.Parameters .Clear();
myCommand.Dispose ();
} if (Iret>0)
return true;
else
return false;
}
============================================CREATE PROCEDURE dbo.instertlog
(
@tel char(15),
@serviceId char(20),
@returnCode varchar(50),
@ErrContent varchar(128),
@operTime datetime
)
AS
declare
@maxid intSELECT @maxID = Max(operaterNo) FROM HL_userlog
IF (@maxID IS NULL)
set @maxID = 1
else
set @maxID = @maxID + 1INSERT INTO HL_userlog
( operaterNo,
tel,
serverId,
returnCode,
ErrContent,
operatertime )
VALUES (@maxid,
@tel,
@serviceId,
@returnCode,
@ErrContent,
@operTime )IF @@error!=0
ROLLBACK
ELSE
COMMIT
GO
===========================
{
SqlConnection myConnection = new SqlConnection(condb.getConnStr());
SqlCommand myCommand = new SqlCommand("instertlog", myConnection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlParameter WorkParm = null;
WorkParm = myCommand.Parameters.Add("@tel", SqlDbType.Char,15);
WorkParm = myCommand.Parameters.Add("@serviceId", SqlDbType.Char,20);
WorkParm = myCommand.Parameters.Add("@returnCode", SqlDbType.VarChar ,50);
WorkParm = myCommand.Parameters.Add("@ErrContent", SqlDbType.VarChar,128);
WorkParm = myCommand.Parameters.Add("@operTime", SqlDbType.DateTime,8);
myCommand.Parameters ["@tel"].Value = feeUserID.Text.Trim();
myCommand.Parameters ["@serviceId"].Value = serviceID;
myCommand.Parameters ["@returnCode"].Value = retCode;//retCode
myCommand.Parameters ["@ErrContent"].Value = retStr;
myCommand.Parameters ["@operTime"].Value = DateTime.Now.GetDateTimeFormats();
int Iret = 0;
try
{
myConnection.Open();
Iret = myCommand.ExecuteNonQuery() ;
}
finally
{
myConnection.Dispose();
myConnection.Close();
myCommand.Parameters .Clear();
myCommand.Dispose ();
} if (Iret>0)
return true;
else
return false;
}
============================================CREATE PROCEDURE dbo.instertlog
(
@tel char(15),
@serviceId char(20),
@returnCode varchar(50),
@ErrContent varchar(128),
@operTime datetime
)
AS
declare
@maxid intSELECT @maxID = Max(operaterNo) FROM HL_userlog
IF (@maxID IS NULL)
set @maxID = 1
else
set @maxID = @maxID + 1INSERT INTO HL_userlog
( operaterNo,
tel,
serverId,
returnCode,
ErrContent,
operatertime )
VALUES (@maxid,
@tel,
@serviceId,
@returnCode,
@ErrContent,
@operTime )IF @@error!=0
ROLLBACK
ELSE
COMMIT
GO
===========================
对象必须实现 IConvertible。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 对象必须实现 IConvertible。源错误:
行 122: {
行 123: myConnection.Open();
行 124: Iret = myCommand.ExecuteNonQuery() ;
行 125: }
行 126: finally
源文件: c:\inetpub\wwwroot\webapplication3\webform1.aspx.cs 行: 124 堆栈跟踪:
[InvalidCastException: 对象必须实现 IConvertible。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
WebApplication3.WebForm1.insertLog(String serviceID) in c:\inetpub\wwwroot\webapplication3\webform1.aspx.cs:124
WebApplication3.WebForm1.Button2_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication3\webform1.aspx.cs:191
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573
你的存储最后使用了事务,而前面没有"BEGIN TRANSACTION"
是这个问题吧,已经ok了没?DateTime.Now可以了。
DateTime.Now.GetDateTimeFormats() 返回的 string[] 数组类型数组类型是无法显式转换成 SqlDbType.DateTime。解决办法:
DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")