我已经在本网站上检查了与此相似的其他问题,但他们的解决方案都没有对我有用。
我在C#中使用SQL来尝试将新记录插入本地Access数据库。目前,我在运行时遇到this错误。
完整的方法:
public static void AddNewMusicFile(MusicFile m)
{
long newID;
int genreID = GenreDB.GetGenreID(m.AUDFIL_GENRE_NM);
String strSQL = String.Empty;
OleDbTransaction transaction = null;
OleDbCommand cm = new OleDbCommand();
OleDbConnection cn = new OleDbConnection(strConnString);
cm.Connection = cn;
cn.Open();
transaction = cn.BeginTransaction();
cm.Connection = cn;
cm.Transaction = transaction;
strSQL = "INSERT INTO AUDIOFILES_MSTR ";
strSQL += "(";
strSQL += "AUDFIL_TT, ";
strSQL += "AUDFIL_RELEASE_DTM, ";
strSQL += "AUDFIL_LENGTH, ";
strSQL += "AUDFIL_GENRE_ID, ";
strSQL += "AUDFIL_TYPE_ID, ";
strSQL += "AUDFIL_ADD_DTM, ";
strSQL += "AUDFIL_STAT_CD ";
strSQL += ") VALUES (";
strSQL += "@AUDFIL_TT, ";
strSQL += "@AUDFIL_RELEASE_DTM, ";
strSQL += "@AUDFIL_LENGTH, ";
strSQL += "@AUDFIL_GENRE_ID, ";
strSQL += "@AUDFIL_TYPE_ID, ";
strSQL += "@AUDFIL_ADD_DTM, ";
strSQL += "@AUDFIL_STAT_CD ";
strSQL += ")";
cm.CommandText = strSQL;
OleDbParameter pm = new OleDbParameter("@AUDFIL_TT", OleDbType.Char, 163);
pm.Direction = ParameterDirection.Input;
pm.Value = m.AUDFIL_TT;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@AUDFIL_RELEASE_DTM", OleDbType.Date);
pm.Direction = ParameterDirection.Input;
pm.Value = m.AUDIL_RELEASE_DTM;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@AUDFIL_LENGTH", OleDbType.Integer);
pm.Direction = ParameterDirection.Input;
pm.Value = m.AUDFIL_LENGTH;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@genreID", OleDbType.Integer);
pm.Direction = ParameterDirection.Input;
pm.Value = genreID;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@AUDFIL_TYPE_ID", OleDbType.Integer);
pm.Direction = ParameterDirection.Input;
pm.Value = m.AUDFIL_TYPE_ID;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@AUDFIL_ADD_DTM", OleDbType.Date);
pm.Direction = ParameterDirection.Input;
pm.Value = m.AUDFIL_ADD_DTM;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@AUDFIL_STAT_CD", OleDbType.VarChar, 1);
pm.Direction = ParameterDirection.Input;
pm.Value = 'A';
cm.Parameters.Add(pm);
cm.ExecuteNonQuery();
cm.CommandText = "SELECT @@IDENTITY AS AUDFIL_ID;";
newID = int.Parse(cm.ExecuteScalar().ToString());
strSQL = "INSERT INTO MUSICFILES_SPLM ";
strSQL += "(";
strSQL += "AUDFIL_ID, ";
strSQL += "MSC_ARTIST_NM, ";
strSQL += "MSC_ALBUM_NM, ";
strSQL += "MSC_PRODUCER_NM, ";
strSQL += "MSC_LABEL_NM ";
strSQL += ") VALUES (";
strSQL += "@AUDFIL_ID, ";
strSQL += "@MSC_ARTIST_NM, ";
strSQL += "@MSC_ALBUM_NM, ";
strSQL += "@MSC_PRODUCER_NM, ";
strSQL += "@MSC_LABEL_NM ";
strSQL += ")";
cm.CommandText = strSQL;
pm = new OleDbParameter("@AUDFIL_ID", OleDbType.Integer);
pm.Direction = ParameterDirection.Input;
pm.Value = newID;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@MSC_ARTIST_NM", OleDbType.Char, 50);
pm.Direction = ParameterDirection.Input;
pm.Value = m.MSC_ARTIST_NM;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@MSC_ALBUM_NM", OleDbType.Char, 163);
pm.Direction = ParameterDirection.Input;
pm.Value = m.MSC_ALBUM_NM;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@MSC_PRODUCER_NM", OleDbType.Char, 50);
pm.Direction = ParameterDirection.Input;
pm.Value = m.MSC_PRODUCER_NM;
cm.Parameters.Add(pm);
pm = new OleDbParameter("@MSC_LABEL_NM", OleDbType.Char, 30);
pm.Direction = ParameterDirection.Input;
pm.Value = m.MSC_LABEL_NM;
cm.Parameters.Add(pm);
cm.ExecuteNonQuery();
transaction.Commit();
}
错误在提交事务之前的最后cm.ExecuteNonQuery()
处触发。
我正在尝试添加数据的表是这样的:
创建音乐文件对象并调用add方法的方法:
private void btnAdd_Click(object sender, EventArgs e)
{
MusicFile m = new MusicFile();
m.MSC_ARTIST_NM = txtArtist.Text;
m.MSC_ALBUM_NM = txtAlbum.Text;
m.MSC_PRODUCER_NM = txtProducer.Text;
m.MSC_LABEL_NM = txtLabel.Text;
m.AUDFIL_TT = txtTitle.Text;
m.AUDIL_RELEASE_DTM = dtpReleaseDate.Value;
m.AUDFIL_TYPE_ID = 1;
m.AUDFIL_LENGTH = (((long)nudHours.Value * 3600) + ((long)nudMinutes.Value * 60) + (long)nudSeconds.Value);
m.AUDFIL_GENRE_NM = cbxGenre.SelectedItem.ToString();
m.AUDFIL_ADD_DTM = Convert.ToDateTime(DateTime.Now.ToShortDateString());
AudioBaseDB.AddNewMusicFile(m);
MessageBox.Show("Record Added Successfully");
}
我现在已经被困在这几个小时了,无法找到导致错误的参数。
答案 0 :(得分:3)
您对三种不同的执行使用相同的命令。问题是你没有清除参数集合,所以你试图在第三次执行时插入第一组参数
....
// First execution with the set of parameters defined before
cm.ExecuteNonQuery();
// Second execution, no parameters required here
cm.CommandText = "SELECT @@IDENTITY AS AUDFIL_ID;";
newID = int.Parse(cm.ExecuteScalar().ToString());
// Before starting to add the parameters for the final INSERT
// clear the collection from the previous ones
cm.Parameters.Clear();
....
在OleDb中,参数不能通过名称识别,而是通过其位置识别 添加了与第一个INSERT相关的第一个参数块意味着OleDb将在执行最终INSERT时将第一个参数添加到集合(@AUDFIL_TT)作为第一个参数占位符(@AUDFIL_ID)的值。幸运的是,数据类型不匹配,您会收到错误消息。如果偶然的话,所有类型的数据类型都是正确的,那么你就会在第二个表中输入错误的数据。
个人注意事项:我不喜欢以这种方式连接命令文本,而是使用带有@字符的verbatim string literal。 我发现它更具可读性。
strSQL = @"INSERT INTO AUDIOFILES_MSTR
(AUDFIL_TT,AUDFIL_RELEASE_DTM,AUDFIL_LENGTH,
AUDFIL_GENRE_ID,AUDFIL_TYPE_ID,AUDFIL_ADD_DTM,
AUDFIL_STAT_CD)
VALUES
(@AUDFIL_TT,@AUDFIL_RELEASE_DTM,@AUDFIL_LENGTH,
@AUDFIL_GENRE_ID, @AUDFIL_TYPE_ID, @AUDFIL_ADD_DTM,
@AUDFIL_STAT_CD)";