我一直在研究一個與數據庫(.mdf)相關的項目。我在Visual Studio中使用C#創建了一些窗體窗體。基本上這些表單一起工作來存儲,更新和刪除我創建的基於服務的數據庫中的數據。 問題是,當我建立的項目,它建立良好,沒有錯誤。它也按照預期將從文本框提供的數據插入到datagridview。但是,只要我停止當前的調試,然後再次運行,之前提供的所有數據都將從datagridview中丟失! 我不明白爲什麼會發生這種情況。任何人都請幫助我。我完全陌生的東西..一點指導將受到熱烈的讚賞。在VS2010中使用基於服務的數據庫Ultimate
當我以前使用MySQL用於相同的目的時,更新的數據將永久存儲到數據庫,但由於我從MySQL遷移到SQL Server的基於服務的數據庫,我得到這樣的混淆錯誤。
......
void loadData()
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("SELECT SNo,Customer_ID, Citizenship_No, Name, Subscription_Date, Phone_No, Location,Locality,Bulbs,Deposit,Monthly_Charge FROM customerinformation;", con);
try
{
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd;
DataTable dt = new DataTable();
adp.Fill(dt);
dataGridViewCustomerInformation.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
try
{
float m_chrg = Convert.ToInt64(textBoxBulbs.Text)*500;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["baikalpik_bidhut_sewaConnectionString"].ToString());
SqlCommand cmd = new SqlCommand("INSERT INTO customerinformation(SNo,Customer_ID,Citizenship_No,Name,Subscription_Date,Location,Locality,Bulbs,Deposit,Phone_No,Monthly_Charge) values('" + textBoxSNo.Text + "','" + textBoxCustomerID.Text + "','" + textBoxCitizenshipNumber.Text + "','" + textBoxName.Text + "','" + textBoxSubscriptionDate.Text + "','" + textBoxLocation.Text + "','" + textBoxLocality.Text + "','" + textBoxBulbs.Text + "','" + textBoxDeposit.Text + "','" + textBoxPhoneNumber.Text + "','" + m_chrg + "')", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
dt = new DataTable();
dt.Load(reader);
con.Close();
dataGridViewCustomerInformation.DataSource = dt;
loadData();
MessageBox.Show("Entry Added!");
fillListbox();
textBoxSNo.Clear();
textBoxBulbs.Clear();
textBoxCitizenshipNumber.Clear();
textBoxCustomerID.Clear();
textBoxDeposit.Clear();
textBoxLocality.Clear();
textBoxLocation.Clear();
textBoxPhoneNumber.Clear();
textBoxName.Clear();
textBoxSubscriptionDate.Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
你有你的中密度板項目上市文件?檢查屬性「複製到輸出目錄」並驗證它沒有設置爲「始終複製」 – Steve 2013-05-04 17:16:54
先生,我的項目中部分滿足了您的建議要求。我在項目文件中列出了mdf,因爲我通過右擊添加了它項目並將其添加爲新項目。但輸出目錄的屬性設置爲始終複製。我有什麼要改變它? 感謝您的快速回復。 :) – nipunshakya 2013-05-04 17:25:15
你爲什麼要在INSERT命令上調用ExecuteReader? INSERT不會返回任何數據,因此可能會導致問題。 – RBarryYoung 2013-05-04 17:34:33