即使成功插入后在数据库中也看不到该值 [英] The value is not seen in the database even after successful insertion

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

问题描述




当我在代码中运行插入语句时,它会很好地执行.由于所有值都是在调试时看到的.我收到消息,表明已成功插入值,但是当我检查数据库时,没有看到任何值.当我再次运行相同的代码时,它告诉我该值已存在于数据库中.但是我看不到数据库中的值.

谁能告诉我为什么会这样?

代码为:

Hi,


When i am running a insert statement in the code it get executed finely. As all values are seen while debugging. I get the message that value is inserted successfully but when i check the database no value is seen in that. And when i again run the same code it gives me the message that this value is already present in the database. But i am not able to see the values in the database.

So can anyone tell me that why this happens?

The code is :

try
{
  if (txtfname.Text == "" || txtlname.Text == "")
  {
    MessageBox.Show("Enter the first name and last name");
  }
  else
  {
    string ClientName = txtlname.Text + txtfname.Text + txtmname.Text;
    Address = txtadd.Text;
    City = txtcity.Text;
    LandlineNo = txtlandline.Text;
    MobNo = txtmobno.Text;
    EmailID = txtEmailID.Text;
    WebSite = txtwebsite.Text;
                        
    SqlCommand command = new SqlCommand("INSERT INTO Client(ClientName,Address,City,LandlineNo,MobNo,EmailID,WebSite)VALUES ('" + ClientName + "','"+Address+"','"+City+"','"+LandlineNo+"','"+MobNo+"','"+EmailID+"','"+WebSite+"')", con);
    con.Open();
    int i = command.ExecuteNonQuery();
    object CID;

    command.CommandText = "Select @@Identity";
    CID = command.ExecuteScalar();

    int ClientID = int.Parse(CID.ToString());

    con.Close();
    DialogResult result = MessageBox.Show("Client Info Added successfully", "Vehicle Info", MessageBoxButtons.OK);
    if (result == DialogResult.OK)
    {
      this.Close();
    }                 

  }
}
catch (Exception x)
{

  throw x;
}

谢谢.

推荐答案

我想解决您的代码中的一些问题.

首先,如果有异常,此代码将使数据库连接保持打开状态.像SqlCommandSqlConnection之类的类实现IDisposable,因此您可以将它们包装在using语句中,该语句在大多数情况下(除了大多数情况下)都调用Dispose方法(在这种情况下,打开的数据库连接最少)您的应用程序问题).因此,您的代码应如下所示:
I''d like to address a few issues with your code.

First of all, this code is leaving database connections open if there is an exception. Classes like SqlCommand and SqlConnection implement IDisposable, so you can wrap them in a using statement which calls the Dispose method in all but the most terminal cases (in which case, an open database connection would be the least of your applications problems). So, your code would look like this:
using (SqlConnection con = new SqlConnection(...){ ... }

第二个问题,如果您不对其执行任何操作,则不要捕获异常.try/catch块是无用的,因为您只是重新抛出了异常.

第三,您的代码很容易受到Sql Injection攻击.使用SQL参数代替显式文本注入.

第四,不要使用SELECT @@IDENTITY.如果要向该表添加一个触发器,并将其插入另一个表,则可以从THAT表中获取identityf.改为使用SELECT SCOPE_IDENTITY().

Second issue, don''t catch an exception if you don''t do anything with it. Your try/catch block is useless because you merely rethrow your exception.

Third, your code is wide open to a Sql Injection attack. Use SQL parameters instead of explicit text injection.

Fourth, don''t use SELECT @@IDENTITY. If you were to add a trigger to this table which inserted into another table, you would get the identityf from THAT table instead. Use SELECT SCOPE_IDENTITY() instead.


您是否有机会使用SQL Server文件(即SQL Server Compact)?如果是这样,您认为您正在查看的数据库可能不是您正在查看的数据库.

好的,那是什么意思呢?好吧,如果您查看项目目录中的数据库文件,则该文件不包含您插入的值,但这不是您的应用程序看到的数据库.相反,当您编译应用程序时,需要一步来查看哪些文件需要复制到输出目录(bin \ debug或bin \ release),并且我敢打赌,如果文件较新,则将该文件标记为复制".这意味着您实际上有2个数据库-一个在项目文件夹中,一个在输出文件夹中.该应用程序看到了这一点.因此,要查看数据,您需要做的就是打开其中一个并查看其中的数据.

希望对您有所帮助.
By any chance, are you using a SQL Server file (i.e. SQL Server Compact)? If so, the database you think you''re looking at might not be the database you are looking at.

OK, so what do I mean by that? Well, if you look at your database file in your project directory, it doesn''t contain the values that you have inserted, but that''s not the database your application sees. Instead, when you compile your application, one stage looks to see what files need copying to the output directory (bin\debug or bin\release), and I''m going to bet that you have that file marked as Copy if newer. This means that you actually have 2 databases - the one in the project folder, and the one in the output folder. The application sees this one. So, to see your data, all you need to do is open that one up and have a look at the data inside it.

I hope this helps.


您的查询是什么?我们应该如何知道错误的出处?
发布您的查询以帮助您.
检查您的数据库fieldsdatatype length.
whats your query?? how should we know where the error is coming??
post your query to help you.
Check your database fields, datatype and length.


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

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