插入,更新和删除查询不起作用,为什么? [英] Insert, Update and Delete Query not working, Why?

查看:74
本文介绍了插入,更新和删除查询不起作用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试使用Visual C#和amp;创建简单的插入更新和删除. SQl Server2005.

在我使用关闭应用程序"重新生成或重新打开它之前,它可以正常工作,但是当我对任何成功给我Msgbox的插入",更新"或删除"查询查询到我的SQL DB(.mdf)时,Msgbox我的数据库仍然没有更新.为什么 ? ? ? ?

这是我的代码:

连接方式:

Hi,

I try to create simple Insert Update and Delete with Visual C# & SQl Server 2005.

Its working fine till I Rebuild or Reopen with Close Application, but when I look into My SQL DB (.mdf) after Any Insert , Update or Delete Query that give me successfull Msgbox My Database still not updated. Why ? ? ? ?

Here is My Code :

Connection :

public void ConnecttoDb()
       {
           string myconn;
           myconn = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\Data.mdf;Integrated Security=True;User Instance=True";
           con = new SqlConnection(myconn);
           cmd.Connection = con;
           try
           {
               con.Open();
               //cmd = new SqlCommand("select name from comp",con);
               MessageBox.Show("Connect Success","Database",MessageBoxButtons.OKCancel);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }



插入按钮:



Insert Button :

private void btnInsert_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "insert into comp values (" + tbNo.Text + ",''" + tbName.Text + "'') ";
            cmd.CommandType = cmd.CommandType;
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Insert Done...", "Insert");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }



更新按钮:



Update Button :

{
            cmd.CommandText = "Update comp SET name='" + tbName.Text + "' where no=" + tbNo.Text + " ";
            cmd.CommandType = cmd.CommandType;
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Update Done...", "Update");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }

        }



新代码(同样不起作用....)



New Code (also not working....)

private void buttonUpdate_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection();                     
            SqlCommand command = new SqlCommand();
            connection.ConnectionString = @"Data Source=.\SQLEXPRESS; AttachDbFilename=" + Application.StartupPath + "\\University.mdf;Integrated Security=True";
            command.Connection = connection;
            command.CommandText = "UPDATE Students SET FirstName=@FirstName, LastName=@LastName, Gender=@Gender, Age=@Age, Address=@Address WHERE StudentID=@StudentID"; 
                                                                         
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@FirstName", textBoxFirstName.Text);
            command.Parameters.AddWithValue("@LastName", textBoxLastName.Text);
            command.Parameters.AddWithValue("@Gender", textBoxGender.Text);
            command.Parameters.AddWithValue("@Age", textBoxAge.Text);
            command.Parameters.AddWithValue("@Address", textBoxAddress.Text); 
            command.Parameters.AddWithValue("@StudentID", textBoxStudentID.Text);     
                                                                        
     try                                                                 
     {                                                                   
         connection.Open();                                              
         int result = command.ExecuteNonQuery();  
                                                                         
         if (result > 0)                                              
             MessageBox.Show("Update Successful!");                      
         else                                                            
             MessageBox.Show("Update Failed!");                          
    }                                                                   
     catch (SqlException ex)                                             
    {                                                                   
         MessageBox.Show("An error occured." + ex);                           
    }                                                                   
    finally                                                             
     {                                                                   
        connection.Close();
         MessageBox.Show("Connection Closed")  ;                                  
     }                            
        }

推荐答案

好像您的数据库文件被复制到每个编译的项目输出文件夹中.因此,修改是在MDF文件的副本中进行的,可能每次编译应用程序时,实际上是将mdf文件的较旧版本复制到输出目录中.
Looks like your database file is copied in the project output folder on each compile. So the modifications are made in the copy of the MDF file and probably every time you compile the application, you actually copy an older version of the mdf file to the output directory.


检查是否为Connection单击插入按钮后打开

如果未打开,则打开连接,执行cmd并最后关闭连接

check is Connection open after click on insert button

if Not open then open connection execute Insert cmd and finally close conection

similarly use for Update also.


修正的代码.

公共无效ConnecttoDb()
{
字符串myconn;
myconn =数据源=.\ SQLEXPRESS; AttachDbFilename =" + Application.StartupPath +"\ Data.mdf;集成安全性= True;用户实例= True";
con =新的SqlConnection(myconn);
con.open(); cmd.Connection = con;
试试
{
//cmd = new SqlCommand(从comp中选择名称",con);
MessageBox.Show(连接成功",数据库",MessageBoxButtons.OKCancel);
}
catch(ex ex例外)
{
MessageBox.Show(ex.Message);
}

插入语句


rectified codes.

public void ConnecttoDb()
{
string myconn;
myconn = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\Data.mdf;Integrated Security=True;User Instance=True";
con = new SqlConnection(myconn);
con.open(); cmd.Connection = con;
try
{
//cmd = new SqlCommand("select name from comp",con);
MessageBox.Show("Connect Success","Database",MessageBoxButtons.OKCancel);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

insert statement


private void btnInsert_Click(object sender, EventArgs e)
        {
            cmd.CommandText = insert into comp values ("'" + tbNo.Text + ",'" + tbName.Text + "') ";
            cmd.CommandType = cmd.CommandType.CommandText;
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Insert Done...", "Insert");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }


这篇关于插入,更新和删除查询不起作用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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