0

InsertParameters 中有一个参数。当我尝试设置它时,我收到一个错误::

不允许从数据类型 sql_variant 到 uniqueidentifier 的隐式转换。使用 CONVERT 函数运行此查询

这是我在 C# 中的代码:

String complexID = Guid.NewGuid().ToString();
SqlDataSource1.InsertParameters["PKComplexID"].DefaultValue = complexID;

有人可以帮忙吗?

4

5 回答 5

1

您需要更改SqlDbTypeon Inserting

protected void SqlDataSource1_Inserting(object sender, 
  SqlDataSourceCommandEventArgs e) { 
    SqlParameter insertedKey = 
      e.Command.Parameters["@PKComplexID"] as SqlParameter; 
    insertedKey.SqlDbType = SqlDbType.UniqueIdentifier; 
}

参考

http://forums.asp.net/t/1712791.aspx/1

对象的唯一标识符字段类型不正确。但不幸的是,我们在 VisualStudio 设计器中没有唯一标识符。所以它给你类型对象。你也不能让它成为唯一标识符,这会引发异常。因此,您必须在 dataSource_Inserting 事件中更改文件后面的此参数类型表单代码。确保它的插入事件。在那里您可以获取任何参数并将 uniqueidentifier 字段类型设置为 uniqueidentifier。

GUID 和数据源控件

于 2012-05-04T17:58:52.420 回答
1

我还没有尝试过,但我的第一反应是:

Guid complexID = Guid.NewGuid();
SqlDataSource1.InsertParameters["PKComplexID"].DefaultValue = complexID;
于 2012-05-04T18:04:49.893 回答
0

我只是将参数类型从对象更改为字符串。解决了。

于 2012-05-07T06:33:34.677 回答
0

试试这个

 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Guid id=Guid.NewGuid();
FileUpload1.SaveAs(Server>MapPath("Your Path"+id+fileName ));
于 2014-07-01T05:55:13.940 回答
0

您不应该转换为字符串。像这样修复您的代码:

Guid complexID = Guid.NewGuid(); 
于 2012-05-04T18:05:09.797 回答