


C#实现上传照片到物理路径,并且将地址保存到数据库的小例子
效果:
思路:
首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库。
数据库:
create table image1 ( ID int identity(1,1) primary key, ImageName varchar(100) , ImageType varchar(20), ImagePath varchar(200) )
代码:
<body> <form id="form1" runat="server"> <p> <table> <tr> <td colspan="2" style="height: 21px"> </td> </tr> <tr> <td style="width: 400px"> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Label ID="label1" runat="server" ForeColor="Red"></asp:Label> </td> <td style="width: 80px"> <asp:Button ID="UploadButton" runat="server" Text="上传图片" OnClick="UploadButton_Click" /> </td> </tr> <tr> <td colspan="2" align="center"> <br /> <br /> <asp:Image ID="Image1" runat="server" Height="118px" Width="131px" /> </td> </tr> </table> </p> </form> </body>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace InExcelOutExcel { public partial class UpWord : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } string SQLString = ConfigurationManager.ConnectionStrings["ConnectionStr"].ToString(); protected void UploadButton_Click(object sender, EventArgs e) { try { using (SqlConnection sqlcon = new SqlConnection(SQLString)) { string FullName = FileUpload1.PostedFile.FileName;//获取图片物理地址 FileInfo fi = new FileInfo(FullName); string name = fi.Name;//获取图片名称 string type = fi.Extension;//获取图片类型 if (type == ".jpg" || type == ".gif" || type == ".bmp" || type == ".png") { string SavePath = Server.MapPath("~\\excel");//图片保存到文件夹下 this.FileUpload1.PostedFile.SaveAs(SavePath + "\\" + name);//保存路径 this.Image1.Visible = true; this.Image1.ImageUrl = "~\\excel" + "\\" + name;//界面显示图片 string sql = "insert into image1(ImageName,ImageType,ImagePath) values('" + name + "','" + type + "','~\\excel" + name + "')"; SqlCommand cmd = new SqlCommand(sql, sqlcon); sqlcon.Open(); cmd.ExecuteNonQuery(); this.label1.Text = "上传成功"; } else { this.label1.Text = "请选择正确的格式图片"; } } } catch (Exception ex) { Response.Write(ex.Message); } } } }
以上就是C#实现上传照片到物理路径,并且将地址保存到数据库的小例子的内容,更多相关内容请关注PHP中文网(www.php.cn)!

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function