c锐利,插入数据库 [英] c sharp, inserting into database

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

问题描述

我正在尝试将数据插入其主键为自动递增的表中.

我遇到的问题是,每当我执行插入操作时,插入操作都会通过,并且在我的应用程序运行时可以查看它,但是一旦停止调试并再次运行,就不会再看到数据.

我还注意到,我未进行的插入未显示在数据库中.

以下是我正在使用的代码:


I am trying to insert data into my table whose primary key is auto increment.

The problem I encounter is that anytime I perform the insert, it goes through and I can view it when my application is running but as soon as I stop debugging and I run again I dont see the data again.

I also notice that the insertion I did does not show up in the database.

Below is the code I am using:


String name, type;
 name = txtname.Text;
 type = txttype.Text;
 
 int qtyToInt = Int32.Parse(txtqty.Text);
 decimal priceToInt = decimal.Parse(txtprice.Text);

            
 shopDataSet.stockRow newStockRow =  shopDataSet1.stock.NewstockRow();
               
                      
 newStockRow.name = name;
 newStockRow.type = type;
 newStockRow.quantity = qtyToInt;
 newStockRow.price = priceToInt;

 this.shopDataSet1.stock.Rows.Add(newStockRow);
 this.stockTableAdapter1.Update(this.shopDataSet1.stock);

推荐答案

然后将其放回原处.当我升级到Win 7(以前是在Vista上)时,我遇到了类似的问题,但是当我部署该应用程序时,它似乎可以正常工作.在我们尝试大胆尝试之前,尝试使用其他方法来保存数据的方法可能是使用sql参数!尝试一下,如果其行为相同,请给我打电话.在您尝试进行此操作之前,请从构建菜单中删除调试一个干净的ur项目,然后再次对其进行调试(确保您同时清除了debug和release)
Okay then put it back as it was before. I had a similar problem when i upgraded to win 7 (was on vista before) but when i deployed the application it seemed to work fine. Before we try drastic measure try to to use a different method to save data may be using sql parameters! Try that and tel me if it behaves the same. Before you do that try it the other way around remove the debug one clean ur project from the build menu and debug it again (make sure you clean for both debug and release )


您的连接字符串很好,但是在C#中,当在字符串文字中使用"\"时是一个特殊字符,因此必须显式地对正在使用文字特殊字符的C#进行tel.因此,有两种方法可以解决您的问题
1.使用转义符-像这样声明您的字符串变量
Your connection string is fine but in C# ''\'' is a special character when used inside string literals so hav to explicitly tel C# that are using special characters literally. So ther two ways to solve your problem
1. Using the escape character- declare your string variable like this
string conString = @".\SQLEXPRESS;AttachDbFilename=|DataDirectory|\shop.mdf;Integrated Security=True;User Instance=True"; // the @ symbol tels csharp to escape all characters


2.使用存储在app.config xml文件中的连接字符串
这是一种更可移植的方式,并且不会造成混乱,因为您不必处理任何类型的转义字符,但请确保已生成app.config(通常在将数据库添加到项目中时执行),然后将其称为lyk this


2. Use the connectionstring stored in the app.config xml file
This is a much more portable way and less messy as you dont hav to deal with escape characters of any kind but make sure app.config was generated (it usually does when u add a database to the project) and then call it lyk this

string conStr = Properties.Settings.Default.ShopConnectionString;
// rest of code 


就是这样


and thats it


好吧,尝试将您的应用程序部署为安装文件,然后安装并运行已安装的应用程序,并在重新启动该应用程序时检查其是否为信息.还尝试重新启动SQL服务frm sql配置管理器
Okay try deploying ur app as a setup file then install and run the installed app and check if it the information iz ther when u re-launch the application. Also try to restart the sql service frm sql configuration manager


这篇关于c锐利,插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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