请帮我看看这个“插入数据库”并进行更正。尽快 [英] Kindly help me take a look at this "insert into database" and make the correction. asap
本文介绍了请帮我看看这个“插入数据库”并进行更正。尽快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
使用 System.Configuration;
使用 System.Data;
public partial class Default7:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
protected void Button1_Click( object sender,EventArgs e)
{
if (!FileUpload1.HasFile) // 验证
{
Response.Write( No file Selected ); 返回;
}
else
{
string filename = FileUpload1.PostedFile.FileName;
// 将图像转换为字节
byte [] imageByte = System.IO.File.ReadAllBytes(filename);
// 在表格中插入数据
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = ConfigurationManager .ConnectionStrings [ lateefconnectionstring]。ToString();
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
string commandText = 插入用户值(@ Surname,@ Lastame,@ username,@ Phone,@ image,getdate,@ Department());
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add( @ Surname,SqlDbType.VarChar);
cmd.Parameters [ @ Surname]。Value = TxtSurname.Text;
cmd.Parameters.Add( @ Lastame,SqlDbType.VarChar);
cmd.Parameters [ @ Lastame]。Value = TxtLastName.Text;
cmd.Parameters.Add( @ username,SqlDbType.VarChar);
cmd.Parameters [ @ username]。Value = TxtUsername.Text;
cmd.Parameters.Add( @ Phone,SqlDbType.VarChar);
cmd.Parameters [ @ Phone]。Value = TxtPhone.Text;
cmd.Parameters.Add( @ image,SqlDbType.VarBinary);
cmd.Parameters [ @ image]。Value = imageByte;
cmd.Parameters.Add( @ Department,SqlDbType.VarChar);
cmd.Parameters [ @ Department]。Value = TxtDpt.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
Response.Write( 已添加数据);
}
}
}
}
解决方案
你还没有发布错误所以我们只是在这里猜测;但是,getdate不起作用。这是一个函数,所以需要使用()来获取GetDate()。
@Department()也不是函数,而是参数。所以使用@Department。
而不是
string commandText =插入用户值(@Surname,@ Lastame,@ username, @电话,@ image,getdate,@ Department());
使用
string commandText =插入用户值(@ Surname,@ Lastame,@ username,@ Phone,@ image,getdate,@ Department);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (!FileUpload1.HasFile) //Validation
{
Response.Write("No file Selected"); return;
}
else
{
string filename = FileUpload1.PostedFile.FileName;
//convert the image into the byte
byte[] imageByte = System.IO.File.ReadAllBytes(filename);
//Insert the Data in the Table
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = ConfigurationManager.ConnectionStrings["lateefconnectionstring"].ToString();
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department())";
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@Surname", SqlDbType.VarChar);
cmd.Parameters["@Surname"].Value = TxtSurname.Text;
cmd.Parameters.Add("@Lastame", SqlDbType.VarChar);
cmd.Parameters["@Lastame"].Value = TxtLastName.Text;
cmd.Parameters.Add("@username", SqlDbType.VarChar);
cmd.Parameters["@username"].Value = TxtUsername.Text;
cmd.Parameters.Add("@Phone", SqlDbType.VarChar);
cmd.Parameters["@Phone"].Value = TxtPhone.Text;
cmd.Parameters.Add("@image", SqlDbType.VarBinary);
cmd.Parameters["@image"].Value = imageByte;
cmd.Parameters.Add("@Department", SqlDbType.VarChar);
cmd.Parameters["@Department"].Value = TxtDpt.Text;
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
Response.Write("Data has been Added");
}
}
}
}
解决方案
You have not posted the error so we're just guessing here; however, getdate will not work. It is a function so needs to be GetDate() with the ().
@Department() is also not a function, but a parameter. So use @Department.
Instead of
string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department())";
use
string commandText = "Insert into users values (@Surname,@Lastame,@username,@Phone,@image,getdate,@Department)";
这篇关于请帮我看看这个“插入数据库”并进行更正。尽快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文