即使在成功执行查询后,也未插入数据 [英] Data is not being inserted even after successfull execution of query

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

问题描述

大家好,



i我正在使用一个Web应用程序,我在按钮点击时将数据插入数据库。这个页面的工作原理(admin.aspx)位于名为''admin''的文件夹中。我在web.config文件中写了下面的connectionstring .....



Hi everyone,

i am working with a web application where i am inserting data to database on button click.the page in which this works(admin.aspx) is located in folder called ''admin''.i have written below connectionstring in web.config file.....

<connectionStrings><br />
    <add name="leave" connectionString="server=PRANAV\SQLEXPRESS;Database=leave management;Integrated Security=true"<br />
      providerName="System.Data.SqlClient" /><br />
    <add name="leave managementConnectionString" connectionString="Data Source=PRANAV\SQLEXPRESS;Initial Catalog="leave management";Integrated Security=True"<br />
      providerName="System.Data.SqlClient" /><br />
  </connectionStrings>





进一步我的代码如下....





SqlConnection conn = new SqlConnection( );

conn.Connection String = System.Configuration.ConfigurationManager.ConnectionStrings [leave] .ConnectionString;

conn.Open();

SqlCommand cmd = new SqlCommand(insert into emp_details VALUES (''+ TextBox1.Text +'',''+ TextBox2.Text +'',''+ TextBox3.Text +'',''+ TextBox5.Text +'',' '+ DropDownList1.SelectedValue +''),conn);









上面的代码工作正常,即使我已调试它...问题是数据没有插入数据库...我无法弄清楚问题在哪里?任何帮助都将是非常appriciated .....如果需要任何额外的细节,让我知道....谢谢.....



further my code is as follows....


SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand("insert into emp_details VALUES(''" + TextBox1.Text + "'',''" + TextBox2.Text + "'',''" + TextBox3.Text + "'',''" + TextBox5.Text + "'',''" + DropDownList1.SelectedValue + "'')", conn);




Above code works fine,even i have debugged it also...the problem is data is not being inserted in the database...i am not able to figure out where the problem is?any help would be greatly appriciated.....if any additional detail is needed,let me know....Thanks.....

推荐答案

你怎么知道代码作品?它显然没有,或者数据库会被更新......



首先要做的是向命令对象添加一个ExecuteNonQuery调用...





然后,当它工作时,不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
How do you know the code works? It clearly doesn''t, or the database would be updated...

The first thing to do is to add an ExecuteNonQuery call to the command object...


Then, when that works, don''t do it like that! Do not 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.






代码看起来很不错,奇怪的是行aint被插入到表。

但是,您可能希望通过在插入语句发生时将int变量作为返回值进行调试,并在返回值大于0时警告消息。



查看以下代码:



Hi,

The code looks pretty ok, its strange that the row aint being inserted in the table.
However, u might want to debug by catching an int variable as return when the insert statement takes place and alert a message when the return value is greater than 0.

have a look at the below code:

public int InsertName(string xname)
{
    int ret;
    SqlCommand cmd = new SqlCommand("Insert into Sample.dbo.tblName values('" + xname + "')", xconn);
    try
    {
        xconn.Open();
        ret = cmd.ExecuteNonQuery();

    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        xconn.Close();

    }
    return ret;

}



,点击按钮或任何其他活动,代码如下:




and on the click of a button or any other event the code will be like below:

private void btnAdd_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtName.Text))
        {
            DataBaseLayer obj = new DataBaseLayer();
            int param=obj.InsertName(txtName.Text);
            if (param > 0)
            {
                MessageBox.Show("your name has been saved..you can now go to the main page");
            }

            NewForm obj2 = new NewForm();
            obj2.Show();


        }
    }





尝试像这样调试,你可能会发现插入语句是否实际执行。

希望它有所帮助。



问候

Anurag



Try to debug like this, you might find whether the insert statement is actually executing or not.
Hope it helps.

Regards
Anurag


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

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