以下是我尝试将项目插入oracle数据库的c#代码。我得到了丢失的逗号错误,但我没有遗漏任何错误。
string queryString = @"INSERT INTO MAILLIST (GRPID, FNAME, MI, LNAME, ADDR1, ADDR2, CITY, STATE,";
queryString += @" ZIP, ZIP4, DOB, SSN, STATUS, DATEADDTOLIST, GENDER, AGE, MEDIA, BANK_ACCT, PRODCODE,";
queryString += @" UNIQUESSN, ACCT_TYPE, ROUTING_NO, EMAIL)";
queryString += @" Values ('" + item.grpid + @"', '" + item.fname + @"', '" + item.mid_init + @"',";
queryString += @" '" + item.lname + @"', '" + item.addr1 + @"', '" + item.addr2 + @"',";
queryString += @" '" + item.city + @"', '" + item.state + @"', '" + item.zip_code + @"',";
queryString += @" '" + item.zip4 + @"', " + item.birth_date + @", '" + item.ssn + @"',";
queryString += @" '" + item.status + @"', " + item.date_added + @", '" + item.gender + @"',";
queryString += @" '" + item.age + @"', '" + item.media + @"', '" + item.bank_acct + @"',";
queryString += @" '" + item.prodcode + @"', '" + item.unique_ssn + "', '" + item.acct_type + @"',";
queryString += @" '" + item.routing_num + @"', '" + item.email + @"')";
输出时,查询看起来像
INSERT INTO MAILLIST (GRPID, FNAME, MI, LNAME, ADDR1, ADDR2, CITY, STATE, ZIP, ZIP4,
DOB, SSN, STATUS, DATEADDTOLIST, GENDER, AGE, MEDIA, BANK_ACCT, PRODCODE, UNIQUESSN,
ACCT_TYPE, ROUTING_NO, EMAIL) Values ('0001190', 'John', '', 'Smith', 'address', '',
'Oak Ridge', 'TN', '37830', '',
04/08/1985 12:00:00 AM, '000000000', 'A ',
07/20/2015 12:00:00 AM,
'M', 29, 'NICK', '0000000', '12345', '00000000', 'S', '000000000', '')
当我尝试使用command.Parameters.Add()插入数据时,我收到一条错误消息:'没有足够的值'
string queryString = @"INSERT INTO MAILLIST (GRPID, FNAME, MI, LNAME, ADDR1, ADDR2, CITY, STATE,";
queryString += @" ZIP, ZIP4, DOB, SSN, STATUS, DATEADDTOLIST, GENDER, AGE, MEDIA, BANK_ACCT, PRODCODE,";
queryString += @" UNIQUESSN, ACCT_TYPE, ROUTING_NO, EMAIL)";
queryString += @" VALUES (:GRPID, :FNAME, :MI, :LNAME, :ADDR1, :ADDR2, :CITY, :STATE";
queryString += @" :ZIP, :ZIP4, :DOB, :SSN, :STATUS, :DATEADDTOLIST, :GENDER, :AGE, :MEDIA, :BANK_ACCT,";
queryString += @" :PRODCODE, :UNIQUESSN, :ACCT_TYPE, :ROUTING_NO, :EMAIL)";
Console.WriteLine(queryString);
using (OracleConnection connection = new OracleConnection(connectionString))
{
OracleCommand command = connection.CreateCommand();
command.CommandText = queryString;
try
{
connection.Open();
command.Parameters.Add(new OracleParameter("GRPID", item.grpid));
command.Parameters.Add(new OracleParameter("FNAME", item.fname));
command.Parameters.Add(new OracleParameter("MI", item.mid_init));
command.Parameters.Add(new OracleParameter("LNAME", item.lname));
command.Parameters.Add(new OracleParameter("ADDR1", item.addr1));
command.Parameters.Add(new OracleParameter("ADDR2", item.addr2));
command.Parameters.Add(new OracleParameter("CITY", item.city));
command.Parameters.Add(new OracleParameter("STATE", item.state));
command.Parameters.Add(new OracleParameter("ZIP", item.zip_code));
command.Parameters.Add(new OracleParameter("ZIP4", item.zip4));
command.Parameters.Add(new OracleParameter("DOB", item.birth_date));
command.Parameters.Add(new OracleParameter("SSN", item.ssn));
command.Parameters.Add(new OracleParameter("STATUS", item.status));
command.Parameters.Add(new OracleParameter("DATEADDTOLIST", item.date_added));
command.Parameters.Add(new OracleParameter("GENDER", item.gender));
command.Parameters.Add(new OracleParameter("AGE", item.age));
command.Parameters.Add(new OracleParameter("MEDIA", item.media));
command.Parameters.Add(new OracleParameter("BANK_ACCT", item.bank_acct));
command.Parameters.Add(new OracleParameter("PRODCODE", item.prodcode));
command.Parameters.Add(new OracleParameter("UNIQUESSN", item.unique_ssn));
command.Parameters.Add(new OracleParameter("ACCT_TYPE", item.acct_type));
command.Parameters.Add(new OracleParameter("ROUTING_NO", item.routing_num));
command.Parameters.Add(new OracleParameter("EMAIL", item.email));
OracleDataReader reader = command.ExecuteReader();
reader.Close();
答案 0 :(得分:2)
问题是因为您还没有正确处理日期。它应该是:
INSERT INTO maillist (grpid,
fname,
mi,
lname,
addr1,
addr2,
city,
state,
zip,
zip4,
dob,
ssn,
status,
dateaddtolist,
gender,
age,
media,
bank_acct,
prodcode,
uniquessn,
acct_type,
routing_no,
email)
VALUES ('0001190',
'John',
'',
'Smith',
'address',
'',
'Oak Ridge',
'TN',
'37830',
'',
TO_DATE ('04/08/1985 12:00:00 AM', 'mm/dd/yyyy hh:mi:ss am'), -- note the use of to_date!
'000000000',
'A ',
TO_DATE ('07/20/2015 12:00:00 AM', 'mm/dd/yyyy hh:mi:ss am'), -- note the use of to_date!
'M',
29,
'NICK',
'0000000',
'12345',
'00000000',
'S',
'000000000',
'');
但是,正如@a_horse_with_no_name所建议的那样,你不应该像这样创建插入语句 - 相反,你应该使用绑定变量。
重新。您的绑定变量感知版本,看起来您在以下:STATE绑定变量后缺少逗号。我认为应该是:
string queryString = @"INSERT INTO MAILLIST (GRPID, FNAME, MI, LNAME, ADDR1, ADDR2, CITY, STATE,";
queryString += @" ZIP, ZIP4, DOB, SSN, STATUS, DATEADDTOLIST, GENDER, AGE, MEDIA, BANK_ACCT, PRODCODE,";
queryString += @" UNIQUESSN, ACCT_TYPE, ROUTING_NO, EMAIL)";
queryString += @" VALUES (:GRPID, :FNAME, :MI, :LNAME, :ADDR1, :ADDR2, :CITY, :STATE,";
queryString += @" :ZIP, :ZIP4, :DOB, :SSN, :STATUS, :DATEADDTOLIST, :GENDER, :AGE, :MEDIA, :BANK_ACCT,";
queryString += @" :PRODCODE, :UNIQUESSN, :ACCT_TYPE, :ROUTING_NO, :EMAIL)";
答案 1 :(得分:2)
坚持参数绑定尝试。你最好继续前进。
您的最新Not enough values
错误似乎是因为:STATE
和:ZIP
之间缺少逗号。