|
|
First of all, don't mark your posts as URGENT. You're not paying anyone for their help, so be prepared to wait.
You haven't stated what type of database you are using. Presumably it is file-based, Access maybe?
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
|
Connection strings for Access require an absolute path to the database file. You will need to construct connection string with the path to the database file yourself in code. You can get the path that your application is executing from by calling Environment.CurrentDirectory . Then append the name of your Access database file.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Thanks a lot I will try it today
Sadaf
|
|
|
|
|
thanks 4 ur kind help
I tried it but not there is a 2nd problem which is as follows:
my code is:
string currentDirectory = System.Environment.CurrentDirectory;
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + currentDirectory + "\\dbLetters.mdb';Persist Security Info=False";
this.oleDbInsertCommand1.Connection = con;
//other parameters
this.oleDbInsertCommand1.Parameters["Subject"].Value = this.txtSubject.Text;
this.oleDbInsertCommand1.Parameters["RefNumber"].Value = this.txtRefNumber.Text;
this.oleDbInsertCommand1.Parameters["RefDate"].Value = this.dateTimePicker1.Value;
this.oleDbInsertCommand1.Parameters["OutwardNumber"].Value = this.txtOutwardNumber.Text;
this.oleDbInsertCommand1.Parameters["OutwardDate"].Value = this.dateTimePicker2.Value;
this.oleDbInsertCommand1.Parameters["FileNumber"].Value = this.txtFileNumber.Text;
this.oleDbInsertCommand1.Parameters["Remarks"].Value = this.txtRemarks.Text;
con.Open();
this.oleDbInsertCommand1.ExecuteNonQuery();
con.Close();
-------------------------------
Upon execution , it is not creating a new record but always updating the last record and I have no clue to why is this happening.
Plz guide me . I shall be really thankful 2 u
Sadaf
|
|
|
|
|
INSERT INTO tblOutgoingLetters
([To], [From], Subject, RefNumber, RefDate, OutwardNumber, OutwardDate, FileNumber, Remarks)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
this is the command that I have in the CommandText property of Command object..but instead of inserting a new record into the table , it is changing the content i.e. updating the last record
Do u have any idea about why is this problem occurring?
Sadaf
|
|
|
|
|
EVERY TIME I OPEN THE SOLUTION FILE TO MAKE SOME CHANGES IN MY PROJECT , THE RECORDS ADDED AFTER USING ENVIRONMENT.CURRENTDIRECTORY ARE DELETED . HOWEVER THE RECORDS THAT WERE INSERTED INTO THE DATABASE BEFORE I USED THIS REMAIN AS IT IS.
WHAT COULD BE THE CAUSE?
Sadaf
|
|
|
|
|
Please don't shout. Pay attention to forum rule no. 6
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
This is actually a problem with you program logic. Insert statement can never update another record. So check your logic may be on insertion the program calls the update function.
Do good and have good.
|
|
|
|
|
i want to store a jpg file in oracle and want to access from vb through ado connection.
|
|
|
|
|
Hope it helps.
BOOL CMyDoc::ImportPic(LPCTSTR filePath)
{
CFile file;
file.Open(filePath,CFile::modeRead);
DWORD len=file.GetLength();
BYTE *pBuf=new BYTE[len+1];
file.ReadHuge(pBuf,len);
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound;
_RecordsetPtr pRs;
pRs.CreateInstance(__uuidof(Recordset));
pRs->Open(_T("picdata"),m_pConn.GetInterfacePtr(),adOpenStatic,adLockOptimistic,adCmdTable);
pRs->AddNew();
pRs->PutCollect(_T("name"),_T("a"));
rgsabound.lLbound=0;
rgsabound.cElements=len;
psa=SafeArrayCreate(VT_UI1,1,&rgsabound);
for (LONG i=0;i<(LONG)len;++i)
SafeArrayPutElement(psa,&i,pBuf++);
varBLOB.vt=VT_ARRAY|VT_UI1;
varBLOB.parray=psa;
pRs->GetFields()->GetItem("pic0")->AppendChunk(varBLOB);
pRs->Update();
pRs->Close();
pRs.Release();
return TRUE;
}
|
|
|
|
|
Why did you do it in C++ when the OP is using VB?
|
|
|
|
|
Yes, only the ado part may help.
|
|
|
|
|
IF EXIST(SELECT 1 FROM t where name='abc' ADN age=20 AND job='worker')
UPDATE t set num=num+1 WHERE name='abc' ADN age=20 AND job='worker'
ELSE INSERT INTO t(id,name,age,job,num) VALUES(NEWID(),'abc',20,'worker',1)
|
|
|
|
|
What is your question?
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
|
Not sure where you can optimize it. Looks pretty tight. Why do you think it needs optimizing? Is it taking a long time to run?
"I guess it's what separates the professionals from the drag and drop, girly wirly, namby pamby, wishy washy, can't code for crap types." - Pete O'Hanlon
|
|
|
|
|
Yes.
Two conditions duplicates.
|
|
|
|
|
I don't think you can improve this. You can't escape duplicating the WHERE clause in the EXISTS and UPDATE queries. It's not really duplication anyway - the UPDATE query is only fired under certain conditions.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
The following will remove the need for the "if exist" statement:
UPDATE t SET num=num+1
WHERE name='abc'
AND age=20
AND job='worker'
IF (@@ROWCOUNT = 0) BEGIN
INSERT INTO t(id,name,age,job,num)
VALUES(NEWID(),'abc',20,'worker',1)
END Additionally you should look at your indexes. I would expect something like:
CREATE UNIQUE INDEX t_idx1 ON t (name, age, job) I would recommend looking at the other SQL that use this table, then change the order of the columns in the above index to best-suit their requirements.
Regards
Andy
|
|
|
|
|
I'm using ADO.
Any way?
Thanks.
|
|
|
|
|
It would slot-in in the same way as your original code sample. However a better way would be to wrap it in a stored procedure:
CREATE PROCEDURE dbo.Insert_t
@Name VARCHAR(40),
@Age SMALLINT,
@Job VARCHAR(40)
AS BEGIN
SET NOCOUNT ON
UPDATE t SET num = num+1
WHERE name = @Name
AND age = @Age
AND job = @Job
IF (@@ROWCOUNT = 0) BEGIN
INSERT INTO t (id, name, age, job, num)
VALUES (NEWID(), @Name, @Age, @Job, 1)
END
RETURN 0
END You can then create an ADO command object, with parameters, to execute it from your code. Note that you will need to add some error-trapping code. I'll let you figure that all out.
Regards
Andy
|
|
|
|
|
Thanks a lot.
|
|
|
|
|
Interesting approach but I wonder if the failed update statement executes faster than the EXISTS statement? Either way, I don't like the approach beyond the novelty. In cases where I really need "extra" performance I separate the stored procedures and let the application decide which one to call, taking advantage of distributed logic.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|