插入查询问题 [英] Insert query problem

查看:81
本文介绍了插入查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在sql server table中插入记录记录插入但没有任何

错误记录不能在表中播种

检查我的代码



我尝试过:



 Dim sqlConnection As New System.Data .SqlClient.SqlConnection(Data Source = .\SQLEXPRESS; AttachDbFilename = | DataDirectory | \Softlinks.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True)
Dim cmd As New System .Data.SqlClient.SqlCommand
cmd.CommandType = System.Data.CommandType.Text
cmd.CommandText =INSERT into Productitem(Productcode,Productname,Productprice)values(12345,'Keyboard',1245)

cmd.Connection = sqlConnection

sqlConnection.Open()
cmd.ExecuteNonQuery()
sqlConnection.Close()

解决方案

由于您说没有生成错误代码,我认为错误成功。但是你应该用调试器仔细检查它。



没有看到新插入数据的一个潜在原因是如果数据库文件包含在你的项目中,它将会每次编译应用程序时都会复制到输出文件夹,从而覆盖之前的所有修改。



要确保不会发生这种情况,请尝试将数据库文件移动到静态输出路径之外的文件夹,例如 C:\ TEMP \TESTDATA 之类的目录,并将连接字符串更改为指向该位置。

  Dim  sqlConnection 作为  New  System.Data.SqlClient.SqlConnection(  Data Source = .\SQLEXPRESS; AttachDbFilename = C:\ TEMP \ TESTDATA \Softlinks.mdf; Integrated Security = True; Connect Timeout = 30; User Instance = True


首先:检查connectionstring!可能的连接字符串列表如下: SQL Server连接字符串 - ConnectionStrings.com [ ^ ]



其次: Never,从不 使用此类查询,因为 SqlInjection [ ^ ]。而不是那样,使用参数化查询!



如何:防止ASP.NET中的注入攻击 [ ^ ]

在SQL Server中编写安全动态SQL [ ^ ]

如何:执行参数化查询 [ ^ ]

使用SQL Server查询和存储过程的参数 [ ^ ]

参数化查询的性能影响 - 简单谈话 [ ^ ]

I trying to insert record in sql server table Record insert but not any
error record not be sown in table
check my code

What I have tried:

Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Softlinks.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
        Dim cmd As New System.Data.SqlClient.SqlCommand
        cmd.CommandType = System.Data.CommandType.Text
        cmd.CommandText = "INSERT into Productitem (Productcode,Productname,Productprice)  values (12345,'Keyboard',1245)"

        cmd.Connection = sqlConnection

        sqlConnection.Open()
        cmd.ExecuteNonQuery()
        sqlConnection.Close()

解决方案

Since you say that no error code is generated I take it that the error succeeds. However you should double check that with the debugger.

One potential reason for not seeing the newly inserted data is that if the database file is included in your project, it will be copied to the output folder each time you compile the application thus overwriting all previous modifications.

To ensure that this does not happen, try moving the database file to a static folder outside your output path, for example to a directory like C:\TEMP\TESTDATA and change the connection string to point to that location.

Dim sqlConnection As New System.Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\TEMP\TESTDATA\Softlinks.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")


First of all: check connectionstring! List of possible connection strings is here: SQL Server connection strings - ConnectionStrings.com[^]

Second of all: Never, NEVER use such of queries, because of SqlInjection[^]. Instead of that, use parameterized queries!

How To: Protect From Injection Attacks in ASP.NET[^]
Writing Secure Dynamic SQL in SQL Server[^]
How to: Perform Parameterized Queries[^]
Using Parameters for SQL Server Queries and Stored Procedures[^]
Performance Implications of Parameterized Queries - Simple Talk[^]


这篇关于插入查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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