即使插入查询成功执行,数据也不会被插入. [英] Data not get inserted even insert query execute successfully.

查看:79
本文介绍了即使插入查询成功执行,数据也不会被插入.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我在VS2008(VB.net)中制作了一个基于窗口的应用程序.
我做了一个简单的代码,我可以在其中插入数据.插入后,我可以看到网格上的数据.当我插入数据时,插入查询运行完美.出于调试目的,我启动了选择查询并在数据集及其显示数据中获取结果也是.但是当进入数据库时​​,我插入的数据没有显示在那里.

Hi all

i made one window base application in VS2008 (VB.net).
i made one simple code where i can insert data.and after inserting i can see that data on grid.when i am inserting data insert query runs perfect.after that for debugging purpose i fire select query and get result in dataset and its show data too.but when go into database my inserted data not showing there.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con_str As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
        Dim con As SqlConnection = New SqlConnection(con_str)
        Dim cmd, cmd2 As SqlCommand
        Dim adp As New SqlDataAdapter
        Dim ds As New DataSet

        With adp
            .SelectCommand = New SqlCommand
            With .SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "test_insert"
                If con.State = ConnectionState.Closed Then
                    con.Open()

                End If
                .Parameters.Add("cust_name", SqlDbType.VarChar).Value = "hello"
                .Connection = con
            End With
            .Fill(ds, "insert")
        End With


        With adp
            .SelectCommand = New SqlCommand
            With .SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "test_select"
                If con.State = ConnectionState.Closed Then
                    con.Open()

                End If
                .Connection = con
            End With
            .Fill(ds, "select")
        End With





    End Sub
End Class



表测试



Table test

cust_id	numeric(18, 0)
cust_name	varchar(50)




我的存储过程





My Store Procedures


ALTER PROCEDURE test_insert
    @cust_name varchar(50)
AS
    insert into test(cust_name) values (@cust_name)
    RETURN









ALTER PROCEDURE test_select
AS
    select * from test
    RETURN

推荐答案

这可能不是问题,但我注意到您的连接字符串指向一个名为Database1.mdf的数据库文件,而不是服务器上的数据库.

可能是因为您正在数据库中查找数据,但应用程序已将其插入数据库文件中?
This might not be the problem but I noticed your connection string points to a database file called Database1.mdf instead of a database on a server.

Might the problem be that you''re looking in a database for the data but the application inserted it in the database file?


嗯,您可能是对的.因为昨天我尝试放置硬路径/直接路径,它正常工作.事实上,我也得到了旧数据.我给出了"bin/debug"路径.因此,我检查了复制到输出目录"选项,但我已经选择了始终复制"选项.但是仍然没有解决问题.
well u might be a right.because yesterday i tried to put hard path/direct path and it works fine.in fact i got my old data too.i gave path of "bin/debug".so i check option of "Copy To Output Directory" but i selected already "Copy always" option.but still problem not solved.


一旦您在ur项目中定义了insert语句的参数,就添加这行代码
cmd.ExecuteNonQuery()
你可以在数据库中看到数据

试试看,看看是否有效

我已经在ASP.NET中尝试过相同的操作,这是一个虚拟插入语句,并且工作得很好,我希望它可以帮助您我的朋友

使用系统;
使用System.Data;
使用System.Configuration;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Web.UI.HtmlControls;
使用System.Data.OleDb;
公共局部类_Default:System.Web.UI.Page
{
受保护的void Page_Load(对象发送者,EventArgs e)
{
}
公共静态字符串getconnection()
{
return @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\ Registration form \ ASP.NET.mdb; Persist Security Info = False";
}
受保护的void Button1_Click(对象发送者,EventArgs e)
{

OleDbConnection con =新的OleDbConnection();
con.ConnectionString = getconnection();
con.Open();
字符串命令=插入mytable值(@ maiid,@ name,@ password)";
OleDbCommand cmd =新的OleDbCommand(command,con);
cmd.Parameters.AddWithValue("@ mailid",txtmailid.Text);
cmd.Parameters.AddWithValue("@ name",txtname.Text);
cmd.Parameters.AddWithValue("@ password",txtpassword.Text);
int x = cmd.ExecuteNonQuery();
如果(x!= 0)
{
//重定向到其他页面,您可以使用消息框
Response.Redirect("Login.aspx");
}

con.Close();
}
}

如果您正确看待的话,这是C#代码,您将可以将其转换为VB.NET代码,因为仅语法不同,其余部分相同..


一旦发现有用,就对我的答案进行评分

谢谢与问候
基:rose:
once u have defined the parameters of the insert statement in ur project add this small line
cmd.ExecuteNonQuery()
and u can see the data in the database

Try this out and see whether it works or not

i have tried the same in ASP.NET this is a dummy insert statement and works just fine i hope it helps u my friend

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string getconnection()
{
return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Registration form\ASP.NET.mdb;Persist Security Info=False";
}
protected void Button1_Click(object sender, EventArgs e)
{

OleDbConnection con = new OleDbConnection();
con.ConnectionString = getconnection();
con.Open();
string command = "insert into mytable values(@maiid,@name,@password)";
OleDbCommand cmd = new OleDbCommand(command, con);
cmd.Parameters.AddWithValue("@mailid", txtmailid.Text);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.Parameters.AddWithValue("@password", txtpassword.Text);
int x= cmd.ExecuteNonQuery();
if (x != 0)
{
// Redirecting to some other page u can use a Messagebox
Response.Redirect("Login.aspx");
}

con.Close();
}
}

This is a C# code if u look at it correctly u will be able to convert it into VB.NET code as only the syntax differs the remaining part is same..


Do Rate my answer once u find it useful

Thanks & Regards
Radix :rose:


这篇关于即使插入查询成功执行,数据也不会被插入.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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