2015-10-07 125 views
-1

我試圖添加一些數據到數據庫中,但它沒有添加數據庫表內,但它也沒有顯示任何異常也我不知道最新的問題試圖將數據添加到數據庫,但它不添加在表

 try 
     { 
     SqlConnection con = new SqlConnection(@"data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"); 
     con.Open();      
     SqlCommand cmd = new SqlCommand("insert into Store(Item,Description,Expences) values('pen','blue','10')", con); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     } 

我的應用程序配置文件

 [![<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="mssqllocaldb" /> 
     </parameters> 
    </defaultConnectionFactory> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
    <connectionStrings> 
    <add name="StoreContext" connectionString="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings> 
</configuration>][1]][1] 

我的表

CREATE TABLE [dbo].[Store] (
    [Id]   INT   IDENTITY (1, 1) NOT NULL, 
    [Item]  VARCHAR (20) NOT NULL, 
    [Description] VARCHAR (100) NULL, 
    [Expences] INT   NOT NULL, 
    PRIMARY KEY CLUSTERED ([Id] ASC) 
); 
+0

任何類型的錯誤? –

+0

如果你正在使用實體框架,那麼爲什麼你使用SqlCommand插入數據 –

+0

沒有錯誤它顯示 – Vinoth

回答

1

嘗試下面的代碼,貝羅w代碼不使用任何工具編寫,因此請嘗試調整代碼。

try 
{ 
      DbContext xyz = new DbContext(); 
      Store oStore = new Store(); 
      oStore.Item = "pen"; 
      oStore.Description = "blue"; 
      oStore.Expences = 10; 
      xyz.Stores.Add(oStore); 
      xyz.SaveChanges(); // never forgot to do SaveChanges 
} 
catch(Exception ex) 
{ 
     MessageBox.Show(ex.Message); 
} 
+0

已經我試過這個...因爲這也沒有錯誤..但沒有存儲在數據庫中 – Vinoth

+0

檢查您的連接字符串,然後可能是它指向任何其他數據庫,您正在檢查 –

+0

'商店accountdata = new Store() { Description = Convert.ToString(desc.Text), Expences = expences, Item = Convert.ToString(item.Content) }; StoreContext sc = new StoreContext(); sc.Stores.Add(accountdata); sc.SaveChanges(); '我試過這個我的數據庫名是Database1 – Vinoth

1

您在請求中有一個錯誤,筆開支是一個整數

SqlCommand cmd = new SqlCommand("insert into Store(Item,Description,Expences) values('pen','blue',10)", con); 
+0

這不應該導致問題。你可以自己嘗試。儘管不把字符串作爲整數傳遞是個好主意。 – Rahul

+0

現在我也試過這個,但現在也沒有添加到數據庫@Rahul – Vinoth