无法使用c#在sql数据库中插入数据,甚至没有错误

时间:2022-09-21 15:38:41

hy I want to insert new record in my database but am unable to do this even code is fully error free, I added following code in my button click event here is my code

hy我想在我的数据库中插入新记录,但我无法做到这一点,即使代码完全没有错误,我在我的按钮点击事件中添加了以下代码这里是我的代码

SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
    SqlCommand cmd;
    SqlDataAdapter adapt;

private void btn_Update_Click(object sender, EventArgs e)
{
    string query="insert into users(Name,Password)values('ubaid','ali')";
    cmd = new SqlCommand(query, con);
    con.Open();
    cmd.ExecuteNonQuery();
    MessageBox.Show("Record Updated Successfully");
    con.Close();
}

I mean insert query is not updating my database, even when I execute my query it return 2 not 0 which means query applied successfully,

我的意思是插入查询不更新我的数据库,即使我执行我的查询它返回2而不是0这意味着查询应用成功,

1 个解决方案

#1


0  

not really an answer but the steps you need to take to see whats going on, so we can help you are a bit longer...

不是真正的答案,而是你需要采取的步骤,看看有什么进展,所以我们可以帮助你更长一点......

you will need to execute the following query, once in your sql management studio, and once in your program ... i suspect the result being different in both cases

你需要执行以下查询,一次在你的sql管理工作室,一次在你的程序中...我怀疑结果在两种情况下都不同

select @@SERVERNAME, @@SERVICENAME, db_name(), SCHEMA_NAME()

on the code side please use this:

在代码方面请使用此:

private void btn_Update_Click(object sender, EventArgs e)
{
//    string query="insert into users(Name,Password)values('ubaid','ali')";
//    cmd = new SqlCommand(query, con);
//    con.Open();
//    cmd.ExecuteNonQuery();
//    MessageBox.Show("Record Updated Successfully");
//    con.Close();

    string query="select @@SERVERNAME, @@SERVICENAME, db_name(), SCHEMA_NAME()";
    cmd = new SqlCommand(query, con);
    con.Open();
    using(var rdr = cmd.ExecuteReader())
    {
        rdr.read();
        MessageBox.Show($"{rdr.GetString(0)}, {rdr.GetString(1)}, {rdr.GetString(2)}, {rdr.GetString(3)} ");
    }
    con.Close();
}

the result should show the name of the server, its instance name, the name of your DB and of the default schema you are using in both cases

结果应显示服务器的名称,实例名称,数据库名称以及在两种情况下使用的默认架构

example result for my testmachine would look like this:

我的testmachine的示例结果如下所示:

srv9, MSSQLSERVER, testdb, dbo

srv9,MSSQLSERVER,testdb,dbo

expectation in your case:

在你的情况下期望:

you will get 2 different results which means that your sql management studio, where you are trying to check if your code did the right thing, is using a different server, instance, database or schema

您将获得2个不同的结果,这意味着您的sql管理工作室,您正在尝试检查您的代码是否正确,是使用不同的服务器,实例,数据库或模式

with the provided information it will be possible to change the used connectionstring so both your clients work on the same database...

使用提供的信息,可以更改使用的连接字符串,以便您的客户端在同一个数据库上工作...

#1


0  

not really an answer but the steps you need to take to see whats going on, so we can help you are a bit longer...

不是真正的答案,而是你需要采取的步骤,看看有什么进展,所以我们可以帮助你更长一点......

you will need to execute the following query, once in your sql management studio, and once in your program ... i suspect the result being different in both cases

你需要执行以下查询,一次在你的sql管理工作室,一次在你的程序中...我怀疑结果在两种情况下都不同

select @@SERVERNAME, @@SERVICENAME, db_name(), SCHEMA_NAME()

on the code side please use this:

在代码方面请使用此:

private void btn_Update_Click(object sender, EventArgs e)
{
//    string query="insert into users(Name,Password)values('ubaid','ali')";
//    cmd = new SqlCommand(query, con);
//    con.Open();
//    cmd.ExecuteNonQuery();
//    MessageBox.Show("Record Updated Successfully");
//    con.Close();

    string query="select @@SERVERNAME, @@SERVICENAME, db_name(), SCHEMA_NAME()";
    cmd = new SqlCommand(query, con);
    con.Open();
    using(var rdr = cmd.ExecuteReader())
    {
        rdr.read();
        MessageBox.Show($"{rdr.GetString(0)}, {rdr.GetString(1)}, {rdr.GetString(2)}, {rdr.GetString(3)} ");
    }
    con.Close();
}

the result should show the name of the server, its instance name, the name of your DB and of the default schema you are using in both cases

结果应显示服务器的名称,实例名称,数据库名称以及在两种情况下使用的默认架构

example result for my testmachine would look like this:

我的testmachine的示例结果如下所示:

srv9, MSSQLSERVER, testdb, dbo

srv9,MSSQLSERVER,testdb,dbo

expectation in your case:

在你的情况下期望:

you will get 2 different results which means that your sql management studio, where you are trying to check if your code did the right thing, is using a different server, instance, database or schema

您将获得2个不同的结果,这意味着您的sql管理工作室,您正在尝试检查您的代码是否正确,是使用不同的服务器,实例,数据库或模式

with the provided information it will be possible to change the used connectionstring so both your clients work on the same database...

使用提供的信息,可以更改使用的连接字符串,以便您的客户端在同一个数据库上工作...