我已经编写了insert命令的代码,以便使用SQL Server 2008在表中插入记录 [英] I have wrriten the code of insert command to insert a record in a table using SQL server 2008

查看:80
本文介绍了我已经编写了insert命令的代码,以便使用SQL Server 2008在表中插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Imports System.Data.SqlClient 
Imports System.Data.SqlClient.SqlCommand
Imports System.IO
Public Class MainWindow
Dim con As New SqlConnection
Dim cmd As New SqlCommand
< pre> Private Sub Insert_Click(sender As Object,e As RoutedEventArgs)处理Insert.Click

尝试
con.ConnectionString =数据源= MARVELL-PC;初始目录=管理;用户ID = abcd
con.Open()
cmd.Connection = con
cmd.CommandText =插入学生(名字,中间名,姓氏,父亲姓名,母亲姓名,职业,DOB ,手机号码,电子邮件ID,地址,国籍)值('& txtbx_student_name.Text&','& txtbx_student_mname.Text&','& txtbx_student_lname.Text&', '&&; txtbx_student_ftname.Text&','& txtbx_student_mtname.Text&','& txtbx_student_occp.Text&','& txtbx_student_dob.Text&',' & txtbx_student_mb.Text&','& txtbx_student_emid.Text&','& txtbx_student_addr.Text&','& txtbx_student_nat.Text&')
cmd.ExecuteNonQuer y()
MessageBox.Show(数据已成功插入)
Catch ex As Exception
MessageBox.Show(在表格中插入记录时出错...& ex.Message,插入记录)
最后
con.Close()
结束尝试
结束子

我尝试过什么:

它给出错误消息
在表中插入记录时出错,用户abcd登录失败

解决方案

这里有很多错误,有些严重,有些非常严重。

1)永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



2)永远不要硬编码连接字符串 - 如果需要改变它们(它们通常与开发和生产或者你遇到严重问题然后你必须在任何地方改变它们。使用配置文件并从那里获取字符串。



3)您的连接字符串错误。这就是它所抱怨的 - 它无法使用空白密码作为该数据库的SQL用户登录您的用户abcd。

试试这个:创建简单的SQL连接字符串 [ ^ ]


Quote:

在表格中插入记录时出错,用户abcd登录失败



 cmd.CommandText =  插入学生(名字,中间名,姓氏,父亲姓名,母亲姓名,职业) ,DOB,手机号码,电子邮件ID,地址,国籍)值('& txtbx_student_name.Text&  ','& txtbx_student_mname.Text&  ','& txtbx_student_lname.Text&  ','& txtbx_student_ftname.Text&  ','& txtbx_student_mtname.Text&  ','& txtbx_student_occp.Text&  ','& txtbx_student_dob.Text&  ','& txtbx_student_mb.Text&  ','& txtbx_student_emid.Text&  ','& txtbx_student_addr.Text&  ','& txtbx_student_nat.Text&  ') 



你的问题不是解决方案,但你有另一个问题

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlCommand
Imports System.IO
Public Class MainWindow
 Dim con As New SqlConnection
    Dim cmd As New SqlCommand
<pre> Private Sub Insert_Click(sender As Object, e As RoutedEventArgs) Handles Insert.Click

        Try
            con.ConnectionString = "Data Source=MARVELL-PC;Initial Catalog=Management;User ID=abcd"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "insert into student(First Name,Middle Name,Last Nameq,Father Name,Mother Name,Occupation,DOB,Mobile No,Email-id,Address,Nationality)values('" & txtbx_student_name.Text & "','" & txtbx_student_mname.Text & "','" & txtbx_student_lname.Text & "','" & txtbx_student_ftname.Text & "','" & txtbx_student_mtname.Text & "','" & txtbx_student_occp.Text & "','" & txtbx_student_dob.Text & "','" & txtbx_student_mb.Text & "','" & txtbx_student_emid.Text & "','" & txtbx_student_addr.Text & "','" & txtbx_student_nat.Text & "')"
            cmd.ExecuteNonQuery()
            MessageBox.Show("Data Inserted Sucessfully")
        Catch ex As Exception
            MessageBox.Show("Error while inserting a record in a table..." & ex.Message, "Insert Records")
        Finally
            con.Close()
        End Try
    End Sub

What I have tried:

It gives the error message
"Error while inserting a record in a table, Login failed for the user abcd"

解决方案

Loads of things wrong here, some serious, some very serious.
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never hard code connection strings - if they need to be changed (and they generally are different for development and production or you have serious problems coming your way later) then you have to change them everywhere. Use a configuration file and fetch your string from there.

3) Your connection string is wrong. That';s what it is complaining about - it can't log in your user "abcd" as an SQL user of that database with a blank password.
Try this: Simple SQL Connection String Creation[^]


Quote:

"Error while inserting a record in a table, Login failed for the user abcd"


cmd.CommandText = "insert into student(First Name,Middle Name,Last Nameq,Father Name,Mother Name,Occupation,DOB,Mobile No,Email-id,Address,Nationality)values('" & txtbx_student_name.Text & "','" & txtbx_student_mname.Text & "','" & txtbx_student_lname.Text & "','" & txtbx_student_ftname.Text & "','" & txtbx_student_mtname.Text & "','" & txtbx_student_occp.Text & "','" & txtbx_student_dob.Text & "','" & txtbx_student_mb.Text & "','" & txtbx_student_emid.Text & "','" & txtbx_student_addr.Text & "','" & txtbx_student_nat.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[^]


这篇关于我已经编写了insert命令的代码,以便使用SQL Server 2008在表中插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆