2012-03-29 71 views
0

我正在用Sql連接創建一個簡單的WPF應用程序。問題是,當我調試 該程序似乎是好的(沒有錯誤),但我無法看到我的數據庫中的數據。在表中看不到我的數據

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(); 

     conn.ConnectionString = @"DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MIKE.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 

     try 
     { 
      SqlCommand SqlCmd = new System.Data.SqlClient.SqlCommand("INSERT INTO [Customer](FirstName,LastName) VALUES(@FirstName,@LastName)", conn); 

      conn.Open(); 

      // Insert code to process data. 
      SqlCmd.Parameters.AddWithValue("@FirstName", CUCFirstName.Text); 
      SqlCmd.Parameters.AddWithValue("@LastName", CUClastName.Text); 

      SqlCmd.ExecuteNonQuery(); 

     } 
     catch (SqlException ex) 
     { 
      MessageBox.Show(ex.Message, ex.Number.ToString()); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
    } 

和我的XAML代碼

<Grid> 
    <Button Content="Add" Height="66" HorizontalAlignment="Left" Margin="324,233,0,0" 
      Name="button1" VerticalAlignment="Top" 
      Width="135" Click="button1_Click" /> 

    <TextBox Height="36" HorizontalAlignment="Left" Margin="74,32,0,0" Name="CUCFirstName" 
      VerticalAlignment="Top" Width="242"> 
    </TextBox> 

    <TextBox Height="39" HorizontalAlignment="Left" Margin="74,74,0,0" Name="CUClastName" 
      VerticalAlignment="Top" Width="242"> 
    </TextBox> 

    <Label Content="First Name" Height="33" HorizontalAlignment="Left" Margin="324,32,0,0" 
      Name="label1" VerticalAlignment="Top" Width="151" /> 

    <Label Content="Last Name" Height="34" HorizontalAlignment="Left" Margin="324,72,0,0" 
      Name="label2" VerticalAlignment="Top" Width="151" /> 
</Grid> 
+0

數據庫正在被複制到Debug \ Bin文件夾中 – 2012-03-29 10:43:37

回答

0

你運行你的數據庫的一個新實例被複制到debug文件夾(因爲它被認爲是資源)的應用程序每次。把你的數據庫,其中的一些在本地硬盤驅動器(如「C:\ Test」)中,並在連接字符串中指定相同的路徑。

+0

謝謝你的回答。我搜索了它,並且我忘記將數據庫名稱放入 ConnectionString中,並且在app.config中 conn.ConnectionString = @「Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | \ MIKE.mdf; ** Database = MIKE **;集成安全性= True;用戶實例= True「; 現在工作 – 2012-03-29 11:58:26

相關問題