本文介绍了请帮我看看这个“插入数据库”并进行更正。尽快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 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( 已添加数据);
}
}
}
}
解决方案



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");
            }
        }
    }
    }
解决方案



这篇关于请帮我看看这个“插入数据库”并进行更正。尽快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 11:50