我允许用户上传文件并为其命名。现在,我只是在数据库中添加一行,内容如下:
Username, DataSetID (GUID), DataSetName, TimeStamp
.
我收到错误
“对象必须实现 IConvertible。”
Guid unique = Guid.NewGuid();
var current = DateTime.Now;
string sql = "INSERT INTO datasets (userid, datasetid, datasetname, timestamp) VALUES (@userid, @datasetid, @datasetname, @timestamp);";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
using (SqlCommand command = new SqlCommand(sql, con))
{
command.Parameters.Add("@userid", SqlDbType.Text);
command.Parameters["@userid"].Value = System.Web.HttpContext.Current.User.Identity.GetUserId();
command.Parameters.Add("@datasetid", SqlDbType.Text);
command.Parameters["@datasetid"].Value = unique;
command.Parameters.Add("@datasetname", SqlDbType.Text);
command.Parameters["@datasetname"].Value = datasetname.ToString();
command.Parameters.Add("@timestamp", SqlDbType.DateTime);
command.Parameters["@timestamp"].Value = current;
con.Open();
command.ExecuteNonQuery();
con.Close();
}
}