Tag: sqlcommand

如何在使用ExecuteNonQuery()时收到错误消息?

我正在以这种方式执行命令: var Command = new SqlCommand(cmdText, Connection, tr); Command.ExecuteNonQuery(); 在命令中有一个错误,但.NET不会抛出任何错误消息。 我怎么知道命令没有正确执行,以及如何获得exception?

如何将List 转换为Sql In语句的SqlParameter?

我似乎对如何使用SqlParameter执行In语句感到困惑。 到目前为止,我有以下代码: cmd.CommandText = “Select dscr from system_settings where setting in @settings”; cmd.Connection = conn; cmd.Parameters.Add(new SqlParameter(“@settings”, settingList)); reader = cmd.ExecuteReader(); settingsList是List 。 当cmd.ExecuteReader() ,由于无法将List映射到“已知的提供者类型”,因此出现ArgumentException 。 如何(安全地)使用SqlCommand执行In查询?

检查数据库中是否存在记录

我正在使用这些代码行来检查记录是否存在。 SqlCommand check_User_Name = new SqlCommand(“SELECT * FROM Table WHERE ([user] = ‘” + txtBox_UserName.Text + “‘) “, conn); int UserExist = (int)check_User_Name.ExecuteScalar(); 但我收到一个错误: Object reference not set to an instance of an object. 我想要做: if (UserExist > 0) // Update record else // Insert record

通过ExecuteNonQuery检查数据库中是否存在表中的记录

在我的程序中,我需要使用if语句检查表中if已存在数据库中的记录。 使用c#我试图通过SQL连接来做到这一点。 因为我认为ExecuteNonQuery(); 命令返回一个整数值,如果我的假设为真,我想知道什么值是真的知道表中是否存在某条记录。 这是我的代码示例: using (SqlConnection sqlConnection = dbUtil.GetSqlConnection(dbUtil.GetConnectionStringByName(“NonConnectionString”))) { using (SqlCommand sqlCommand = new SqlCommand(“SELECT * from users where user_name like ‘Adam’ AND password like ‘123456’”, sqlConnection)) { sqlresult = sqlCommand.ExecuteNonQuery(); } } 考虑到sqlresult之前在main中初始化为int sqlresult; 所以我想知道,如果这个用户’Adam’存在于数据库中。 如果他存在,那么我想继续进行’if’声明,例如: if(sqlresult == 0) { MessageBox.Show(“Adam exists!”); } 所以我只是不知道它应该返回的整数,我要么不确定这是正确的方法来做到这一点。 谢谢。

带有using语句的SqlCommand

我看到在大多数样本中,SqlCommand都是这样使用的 using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand(“Select ID,Name From Person”, con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } 我知道为什么我们使用“使用”声明。 但是SqlCommand没有包含Close()方法,所以我们应该在using语句中使用它

C#SQL插入命令

谁能告诉我以下2种插入记录的方式可以创造更好的性能? 情况1 SqlCommand cmd = new SqlCommand(); for (int i = 0; i < 10000; i++) { cmd = new SqlCommand("insert into test(id, name) value('" + i + "', '" + i + "')"); cmd.ExecuteNonQuery(); } 案例2 string sql = null; for (int i = 0; i < 10000; i++) { sql += "insert into […]

使用SQLCommand插入C#

将数据插入数据库的最佳方法是什么? 这就是我所拥有的,但这是错的.. cmd.CommandText = “INSERT INTO klant(klant_id,naam,voornaam) VALUES(@param1,@param2,@param3)”; cmd.Parameters.Add(new SqlParameter(“@param1”, klantId)); cmd.Parameters.Add(new SqlParameter(“@param2”, klantNaam)); cmd.Parameters.Add(new SqlParameter(“@param3”, klantVoornaam)); 该函数将数据添加到listBox中 http://www.pictourl.com/viewer/37e4edcf (link is dead) 但不进入数据库.. http://www.pictourl.com/viewer/4d5721fc (link is dead) 全function: private void Form1_Load(object sender, EventArgs e) { conn2 = new SqlConnection(); conn2.ConnectionString = ConfigurationManager.ConnectionStrings[“connSpionshopString”].ConnectionString; } private void button2_Click(object sender, EventArgs e) { string sqlCmd = “SELECT naam,voornaam,klant_id […]

SqlCommand参数添加与AddWithValue

我什么时候应该使用Parameters. Add/AddWithValue Parameters. Add/AddWithValue ? 在下面的MSDN示例中,它们使用Parameters.Add作为int ,使用Parameters.AddWithValue作为string command.Parameters.Add(“@ID”, SqlDbType.Int); command.Parameters[“@ID”].Value = customerID; command.Parameters.AddWithValue(“@demographics”, demoXml); 什么是最适合datetime

MySqlCommand Command.Parameters.Add已过时

我在visual studio 2010中制作了一个C#windows Form Application。 该应用程序正在连接到mysql数据库,我想在其中插入数据。 我现在有这部分代码: MySqlConnection connection; string cs = @”server=server ip;userid=username;password=userpass;database=databse”; connection = new MySqlConnection(cs); connection.Open(); MySqlCommand command = new MySqlCommand(); string SQL = “INSERT INTO `twMCUserDB` (`mc_userName`, `mc_userPass`, `tw_userName`, `tw_userPass`) VALUES (‘@mcUserName’, ‘@mcUserPass’, ‘@twUserName’, ‘@twUserPass’)”; command.CommandText = SQL; command.Parameters.Add(“@mcUserName”, mcUserNameNew); command.Parameters.Add(“@mcUserPass”, mcUserPassNew); command.Parameters.Add(“@twUserName”, twUserNameNew); command.Parameters.Add(“@twUserPass”, twUserPassNew); command.Connection = connection; command.ExecuteNonQuery(); connection.Close(); […]