我正在用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>
數據庫正在被複制到Debug \ Bin文件夾中 – 2012-03-29 10:43:37