在C#日期时间插入和更新方面需要帮助
*/
con.Open();
cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic) VALUES ('" + name.Text + "','" + father.Text + "','" + mother.Text + "','" + surname.Text + "','" + phone.Text + "','" + email.Text + "','" + grno.Text + "','" + course.Text + "','" + rollno.Text + "','" + Convert.ToDateTime(yearofjoin.Text) + "','" + comboBox1sem.Text + "','" + comboBox1blood.Text + "','" + address.Text + "',@pic)", con);
MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
cmd.Parameters.AddWithValue("@pic", pic);
j = cmd.ExecuteNonQuery();
更新代码
update code
cmd = new SqlCommand(@"UPDATE student_details SET name='" + uname.Text + "',father='" + ufather.Text + "',mother='" + umother.Text + "',surname='" + usurname.Text + "',phone='" + uphone.Text + "',email='" + uemail.Text + "',course='" + ucourse.Text + "',rollno='" + urollno.Text + "',yearofjoin='" + uyearofjoin.Text + "',state='"+comboBox1usem.Text+"',blood='"+comboBox1ublood.Text+"',address='"+uaddress.Text+"',pic=@img where (grno='" + ugrno.Text + "')", con);
MemoryStream stream = new MemoryStream();
pictureBox1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
cmd.Parameters.AddWithValue("@img", pic);
j = cmd.ExecuteNonQuery();
if (j > 0)
{
MessageBox.Show("Record Updated Successfully...");
}
ClearingText();
con.Close();
我尝试过:
任何人都可以重写插入代码并为我更新代码
What I have tried:
can anyone rewrite the insert code and update code for me
cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic) VALUES ('" + name.Text + "','" + father.Text + "','" + mother.Text + "','" + surname.Text + "','" + phone.Text + "','" + email.Text + "','" + grno.Text + "','" + course.Text + "','" + rollno.Text + "','" + Convert.ToDateTime(yearofjoin.Text) + "','" + comboBox1sem.Text + "','" + comboBox1blood.Text + "','" + address.Text + "',@pic)", con);
不是你问题的解决方案,而是你遇到的另一个问题。
永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。
名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。
SQL注入 - 维基百科 [ ^ ]
SQL注入 [ ^ ]
按示例进行SQL注入攻击 [ ^ ]
PHP:SQL注入 - 手册 [ ^ ]
SQL注入预防备忘单 - OWASP [ ^ ]
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[^]
格式化sql查询字符串易受攻击到 SQL注入 [ ^ ]攻击
总是使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]
Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
try like this
SqlCommand cmd = new SqlCommand("INSERT INTO student_details (name, father, mother, surname, phone, email, grno, course, rollno, yearofjoin,state,blood,address,pic) VALUES ( @name, @father, @mother, @surname, @phone, @email, @grno, @course, @rollno, @yearofjoin,@state,@blood,@address,@pic)");
cmd.Parameters.AddWithValue("@name",name.Text);
cmd.Parameters.AddWithValue("@father",father.Text);
cmd.Parameters.AddWithValue("@mother",mother.Text);
cmd.Parameters.AddWithValue("@surname",surname.Text);
cmd.Parameters.AddWithValue("@email",email.Text);
cmd.Parameters.AddWithValue("@grno",grno.Text);
cmd.Parameters.AddWithValue("@course",course.Text);
cmd.Parameters.AddWithValue("@rollno",rollno.Text);
cmd.Parameters.AddWithValue("@yearofjoin",Convert.ToDateTime(yearofjoin.Text));
cmd.Parameters.AddWithValue("@state",comboBox1sem.Text);
cmd.Parameters.AddWithValue("@blood",comboBox1blood.Text);
cmd.Parameters.AddWithValue("@address",address.Text);
cmd.Parameters.AddWithValue("@pic", pic);
使用 DateTime.TryParseExact Method [ ^ ]从已知的字符串格式转换日期时间。
use DateTime.TryParseExact Method [^] to convert datetime from a known string format.
据我所知,你不能在内联中传递datetime查询,使用存储过程,这是一种更好的方法来执行保存和更新。
As per my knowledge you cannot pass datetime in inline query, use stored procedure which a better way to perform save and update.