如何将图片添加到SQL数据库

编程入门 行业动态 更新时间:2024-10-23 17:34:39
本文介绍了如何将图片添加到SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个存储过程存储到5个表

USE [STTKDevelopers] GO / * **** *对象:StoredProcedure [dbo]。[AddIndividualParty]脚本日期:05/28/2013 13:22:58 ****** / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER 程序 [dbo]。[AddIndividualParty] @ Student_Number nvarchar ( 15 ), @ Candidate_ First_Name nvarchar ( 50 ), @Candidate_Last_Name nvarchar ( 50 ), @ Candidate_Photo image = null, @ Party_Manifesto nvarchar ( 50 )= null, @ Portfolio_Name nvarchar ( 50 )= null, @ Site_Name nvarchar ( 50 )= null, @ Faculty_Name nvarchar ( 50 )= null AS if ( @ Candidate_Photo IS NOT NULL ) BEGIN INSERT INTO CANDIDATE(Student_Number,Candidate_First_Name,Candidate_Last_Name,Candidate_Photo) VALUES ( @ Student_Number , @ Candidate_First_Name , @ Candidate_Last_Name , @ Candidate_Photo ) END if ( @ Party_Manifesto IS NOT NULL ) BEGIN INSERT INTO PARTY(Party_Manifesto) VALUES ( @ Party_Manifesto ) END if ( @ Portfolio_Name IS NOT NULL ) BEGIN INSERT INTO PORTFOLIO(Portfolio_Name) VALUES ( @ Portfolio_Name ) END if ( @ Site_Name IS NOT NULL ) BEGIN INSERT INTO SITE(Site_Name) VALUES ( @ Site_Name ) END if ( @ Faculty_Name IS NOT NULL ) BEGIN INSERT INTO FACULTY(Faculty_Name) VALUES ( @ Faculty_Name ) END

以下是您点击添加个人时的代码

protected void AddIndividual_Click( object sender,EventArgs e) { SqlConnection connection = null ; try { FileUpload img =(FileUpload)FileUpload1; 字节 [] imgByte = null ; if (img.HasFile&& img.PostedFile!= null ) { // 创建PostedFile HttpPostedFile文件= FileUpload1。 PostedFile; // 使用文件len创建字节数组 imgByte = new Byte [File.ContentLength]; // 强制控件在数组中加载数据 File.InputStream。读取(imgByte, 0 ,File.ContentLength); } // 将员工姓名和图像插入db string conn = ConfigurationManager.ConnectionStrings [ ConnString].ConnectionString; connection = new SqlConnection(conn); connection.Open(); SqlCommand cmd = new SqlCommand( AddIndividualParty ,连接); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue( @ Student_Number,txtStudentNumber.Text ); cmd.Parameters.AddWithValue( @ Candidate_First_Name,txtfirstname.Text); cmd.Parameters.AddWithValue( @ Candidate_Last_Name,txtlastname.Text); cmd.Parameters.AddWithValue( @ Candiadate_Photo,imgByte); cmd.Parameters.AddWithValue( @ Party_Manifesto,FileManifesto); cmd.Parameters.AddWithValue( @ Portfolio_Name,DropDownPortfolio.Text); cmd.Parameters.AddWithValue( @ Site_Name,txtYourSiterep.Text); cmd.Parameters.AddWithValue( @ Faculty_Name,txtYourFaculty.Text); int id = Convert.ToInt32(cmd.ExecuteScalar()); // lblResult.Text = String.Format(员工ID为{0},id); } catch (例外) { // lblResult.Text =出现错误; t } finally { connection.Close(); }

这是我的connectionString

public string ConnString { 获取 { 返回 数据源= CEEYER-PC;初始目录= STTKDevelopers;集成安全性= True;; } }

这是错误。 第193行显示错误和message是未将对象引用设置为对象的实例。 '/ STTK1''应用程序中的服务器错误。 ------- -------------------------------------------------- ----------------------- 对象参考不是设置为对象的实例。 描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。 来源错误: 第191行:最后第192行:{ 第193行:connection.Close(); 第194行:} 第195行: 源文件:c:\ Users \ SHONISANI \Desktop \STTK1 \Student\AddAsIndividual.aspx。 cs行:193

我一直在试用这段代码两天没有运气,我希望你能帮助我克服这个错误。 Thanx

解决方案

如果你想找到空引用错误的原因,请阅读它:System.NullReferenceException - Object引用未设置为对象的实例。 3个常见原因。 [ ^ ] 我建议你以这种方式在程序开始时声明并初始化 conn 变量(避免null):

受保护 void AddIndividual_Click( object sender,EventArgs e) { SqlConnection connection = new SqlConnection(conn) 尝试 { // 您的代码 } catch { // catch block }

最后检查 SQLConnection.State [ ^ ]属性,然后再尝试关闭它;)

最后 { if (connection == ConnectionState.Open) { connection.Close(); // 其他说明 } } }

ConnectionState Enumeration [ ^ ]

嗨... 看到这个链接,它可能对你有帮助。 如何存储&使用asp c#在mysql数据库中检索图像[ ^ ] 谢谢你。

它不适用于您的SP。 您缺少填充参数集合中使用的任何变量的值。 检查imgByte和FileManifesto,因为其中任何一个都是空白的。 检查FileManifesto并确认它应该没有传递为null。

I have a store procedure to store to 5 tables

USE [STTKDevelopers] GO /****** Object: StoredProcedure [dbo].[AddIndividualParty] Script Date: 05/28/2013 13:22:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[AddIndividualParty] @Student_Number nvarchar(15), @Candidate_First_Name nvarchar(50), @Candidate_Last_Name nvarchar(50), @Candidate_Photo image =null, @Party_Manifesto nvarchar(50)=null, @Portfolio_Name nvarchar(50)=null, @Site_Name nvarchar(50)=null, @Faculty_Name nvarchar(50)=null AS if(@Candidate_Photo IS NOT NULL) BEGIN INSERT INTO CANDIDATE(Student_Number, Candidate_First_Name, Candidate_Last_Name, Candidate_Photo) VALUES(@Student_Number, @Candidate_First_Name, @Candidate_Last_Name, @Candidate_Photo) END if(@Party_Manifesto IS NOT NULL) BEGIN INSERT INTO PARTY(Party_Manifesto) VALUES(@Party_Manifesto) END if(@Portfolio_Name IS NOT NULL) BEGIN INSERT INTO PORTFOLIO(Portfolio_Name) VALUES(@Portfolio_Name) END if(@Site_Name IS NOT NULL) BEGIN INSERT INTO SITE(Site_Name) VALUES(@Site_Name) END if(@Faculty_Name IS NOT NULL) BEGIN INSERT INTO FACULTY(Faculty_Name) VALUES(@Faculty_Name) END

Below is the code when you click to add individual

protected void AddIndividual_Click(object sender, EventArgs e) { SqlConnection connection = null; try { FileUpload img = (FileUpload)FileUpload1; Byte[] imgByte = null; if (img.HasFile && img.PostedFile != null) { //To create a PostedFile HttpPostedFile File = FileUpload1.PostedFile; //Create byte Array with file len imgByte = new Byte[File.ContentLength]; //force the control to load data in array File.InputStream.Read(imgByte, 0, File.ContentLength); } // Insert the employee name and image into db string conn = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString; connection = new SqlConnection(conn); connection.Open(); SqlCommand cmd = new SqlCommand("AddIndividualParty", connection); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Student_Number", txtStudentNumber.Text); cmd.Parameters.AddWithValue("@Candidate_First_Name", txtfirstname.Text); cmd.Parameters.AddWithValue("@Candidate_Last_Name", txtlastname.Text); cmd.Parameters.AddWithValue("@Candiadate_Photo", imgByte); cmd.Parameters.AddWithValue("@Party_Manifesto", FileManifesto); cmd.Parameters.AddWithValue("@Portfolio_Name", DropDownPortfolio.Text); cmd.Parameters.AddWithValue("@Site_Name", txtYourSiterep.Text); cmd.Parameters.AddWithValue("@Faculty_Name", txtYourFaculty.Text); int id = Convert.ToInt32(cmd.ExecuteScalar()); //lblResult.Text = String.Format("Employee ID is {0}", id); } catch (Exception) { // lblResult.Text = "There was an error";t } finally { connection.Close(); }

And this is my connectionString

public string ConnString { get { return "Data Source=CEEYER-PC;Initial Catalog=STTKDevelopers;Integrated Security=True;"; } }

This is the Error m getting. Line 193 is the one showing the error and the message is Object reference not set to an instance of an object. Server Error in ''/STTK1'' Application. -------------------------------------------------------------------------------- Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error:

Line 191: finally Line 192: { Line 193: connection.Close(); Line 194: } Line 195: Source File: c:\Users\SHONISANI\Desktop\STTK1\Student\AddAsIndividual.aspx.cs Line: 193

I''ve been trying this code for two days with no luck and I hope you will help me to overcome this error. Thanx

解决方案

If you want to find the reason of "Null reference error", please read it: System.NullReferenceException – Object reference not set to an instance of an object. 3 common causes.[^] I would suggest you to declare and initialize conn variable at the beginning of your procedure in this way (avoids null):

protected void AddIndividual_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection(conn) try { //your code } catch { //catch block }

and finally to check SQLConnection.State[^] property before you''ll try to close it ;)

finally { if (connection == ConnectionState.Open) { connection.Close(); //other instructions } } }

ConnectionState Enumeration[^]

Hi... see this link,its may helpful to u. How to store & retrieve images in mysql database using asp c#[^] thank u.

Hi, It is not realted to your SP. You are missing to fill value for any variable which you are using in parameter collection. Check for imgByte and FileManifesto as any one of them is passed as blank. Check for FileManifesto and confirm that it should not passed as null.

更多推荐

如何将图片添加到SQL数据库

本文发布于:2023-10-27 03:01:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据库   图片   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!