【发布时间】:2021-06-23 08:06:31
【问题描述】:
所以我试图让我的数据库存储在从带有会话变量值的文本框中获取的值中。当我尝试在我的数据库表演示中添加它时:-
CREATE TABLE [dbo].[demo]
(
[Id] INT NOT NULL identity(1,1) PRIMARY KEY,
[email] VARCHAR (50) NULL,
[Name] VARCHAR(50) NULL,
[ContentType] NVARCHAR(50) NULL,
[Data] VARBINARY(MAX) NULL
)
我得到这个异常:- System.Data.SqlClient.SqlException: '只有在使用列列表并且 IDENTITY_INSERT 为 ON 时,才能为表 'demo' 中的标识列指定显式值。'
这是我的 aspx.cs 代码:-
string constr = //My data Source
protected void Page_Load(object sender, EventArgs e)
{
text1.Text = "" + Session["email"];
}
protected void Button1_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con = new SqlConnection(constr))
{
string query = INSERT INTO demo VALUES (@email, @Name, @ContentType, @Data);
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@email", text1.Text);
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
有什么方法可以将携带会话变量值的文本框获取到数据库中?
【问题讨论】: