需要帮助我的项目 [英] Need help for my project
问题描述
我无法将图像上传到数据库中,它总是说找不到路径的一部分
这是我的代码
我尝试了什么:
使用System;
使用System.Data;
使用System.Windows.Forms;
使用System.IO;
使用System.Data.SqlClient;
命名空间LOGIN
{
public partial class add_student_info:Form
{
SqlConnection con = new SqlConnection(@Data Source =( LocalDB)\ MSSQLLocalDB; AttachDbFilename = C:\Users\Aldwin\Desktop\ojt \LOGIN \LOGIN \Database.mdf; Integrated Security = True);
string pwd = Class1.GetRandomPassword(20);
string wanted_path;
public add_student_info()
{
InitializeComponent();
}
private void button1_Click(object sender,EventArgs e)
{
pwd = Class1.GetRandomPassword(20);
wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
DialogResult result = openFileDialog1.ShowDialog();
openFileDialog1.Filter =JPEG文件(* .jpeg)| * .jpeg | PNG文件(* .png)| * .png | JPG文件(* .jpg)| * .jpg | GIF文件(*。 GIF)| * .gif注意;
if(result == DialogResult.OK)//测试结果。
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
//pictureBox1.ImageLocation = @.. \\\\student_images\+ pwd +.jpg;
}
private void textBox1_TextChanged(object sender,EventArgs e)
{
}
private void button2_Click(object sender,EventArgs e)
{
try
{
string img_path;
File.Copy(openFileDialog1.FileName,wanted_path +\\student_image\\+ pwd +.jpg);
img_path =student_image\\+ pwd +.jpg;
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText =插入student_info值('+ textBox1.Text +','+ img_path.ToString()+','+ textBox2.Text +','+ textBox3 .Text +','+ textBox4.Text +','+ textBox5.Text +','+ textBox6.Text +');
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(记录已成功插入);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
}
cmd.CommandText = 插入student_info值(' + textBox1.Text + ',' + img_path.ToString()+ ',' + textBox2.Text + ',' + textBox3.Text + ',' + textBox4.Text + ',' + textBox5.Text + ',' + textBox6.Text + ');
不是您问题的解决方案,而是您遇到的另一个问题。
永远不要构建SQL查询连接字符串。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。
名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。
SQL注入 - 维基百科 [ ^ ]
SQL注入 [ ^ ]
按示例进行SQL注入攻击 [ ^ ]
PHP:SQL注入 - 手册 [ ^ ]
SQL注入预防备忘单 - OWASP [ ^ ]
I can't upload an image into the database it always says " Could not find a part of the path"
Here's my code
What I have tried:
using System; using System.Data; using System.Windows.Forms; using System.IO; using System.Data.SqlClient; namespace LOGIN { public partial class add_student_info : Form { SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Aldwin\Desktop\ojt\LOGIN\LOGIN\Database.mdf;Integrated Security=True"); string pwd=Class1.GetRandomPassword(20); string wanted_path; public add_student_info() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { pwd = Class1.GetRandomPassword(20); wanted_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())); DialogResult result = openFileDialog1.ShowDialog(); openFileDialog1.Filter = "JPEG Files(*.jpeg)|*.jpeg|PNG Files (*.png )|*.png |JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; if (result == DialogResult.OK) //Test result. { pictureBox1.ImageLocation = openFileDialog1.FileName; pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; } //pictureBox1.ImageLocation = @"..\..\student_images\" + pwd + ".jpg"; } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { try { string img_path; File.Copy(openFileDialog1.FileName, wanted_path + "\\student_image\\" + pwd + ".jpg"); img_path = "student_image\\" + pwd + ".jpg"; con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into student_info values('" + textBox1.Text + "','" + img_path.ToString() + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')"; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record Inserted Successfully"); } catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); } } } }
cmd.CommandText = "insert into student_info values('" + textBox1.Text + "','" + img_path.ToString() + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')";
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
这篇关于需要帮助我的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!