Showing posts with label db connection. Show all posts
Showing posts with label db connection. Show all posts

Wednesday, November 29, 2006

connecting btrieve database with vb6

you have an old version of btrieve and you want to read data from it?? well, you'r luck if you read this message in the beginning of your search... i lost 3 days of intensive searching.. and finaly found it. and here i share it with you...
well, open vb program and bla bla bla ...
now you should define some consts for easy work:
i suggest you to create a separate modul for this code (that what i did)
DefInt A-Z
Global Const BOPEN = 0
Global Const BCLOSE = 1
Global Const BINSERT = 2
Global Const BUPDATE = 3
Global Const BDELETE = 4
Global Const BGETEQUAL = 5
Global Const BGETNEXT = 6
Global Const BGETGREATEROREQUAL = 9
Global Const BGETFIRST = 12
Global Const BCREATE = 14
Global Const BSTAT = 15
Global Const BSTOP = 25
Global Const BVERSION = 26
Global Const BRESET = 28
Global Const KEY_BUF_LEN = 255

Rem Key FlagsGlobal Const DUP = 1
Global Const MODIFIABLE = 2
Global Const BIN = 4
Global Const NUL = 8
Global Const SEGMENT = 16
Global Const SEQ = 32
Global Const DEC = 64
Global Const SUP = 128
Rem Key Types
Global Const EXTTYPE = 256
Global Const MANUAL = 512
Global Const BSTRING = 0
Global Const BINTEGER = 1
Global Const BFLOAT = 2
Global Const BDATE = 3
Global Const BTIME = 4
Global Const BDECIMAL = 5
Global Const BNUMERIC = 8
Global Const BZSTRING = 11
Global Const BAUTOINC = 15

' now declare the function BTRCALL with do all the I\O with the database
Private Declare Function BTRCALL Lib "wbtrv32.dll" (ByVal OP, ByVal Pb$, Db As Any, DL As Long, Kb As Any, ByVal Kl, ByVal Kn) As Integer

' define some data types for working with the database files
Type typ_byte4 f1(1 To 4) As ByteEnd TypeRem ***************************************************************************
Rem Btrieve Structures
Type KeySpec
KeyPos As Integer
KeyLen As Integer
KeyFlags As Integer
KeyTot As typ_byte4
KeyType As String * 1
Reserved As String * 5
End Type

Type FileSpec
RecLen As Integer
PageSize As Integer
IndxCnt As Integer
NotUsed As String * 4
FileFlags As Integer
Reserved As String * 2
Allocation As Integer
KeyBuf(0 To 1) As KeySpec
End Type

Type StatFileSpecs
RecLen As Integer
PageSize As Integer
IndexTot As Integer
RecTot As typ_byte4
FileFlags As Integer
Reserved As String * 2
UnusedPages As Integer
KeyBuf(0 To 1) As KeySpec
End Type

Type RecordBuffer
' here you should define the structure of the file that you are reading.. in this example i have 3 'fields, one is an id (int) description and another float type
'change this to your data structure
profID As Integer
profDesc As String * 28
profPrice As Double
End Type

Type VersionBuf
Major As Integer
Minor As Integer
Engine As String * 1
End Type

Type typ_PosBlk
f1(1 To 128) As Byte
End Type

' defining variables with the types we created
Global FileBuf As FileSpec
Global DataBuf As RecordBuffer
Global StatFileBuffer As StatFileSpecs
Global BufLen As Long
Global DBLen As Integer

Rem*******Added to Open multiple files
Public iMaxRuns As Integer
Public bFilesCreated As Boolean

Sub PrintLB(Item As String)
' function for printing data on screen
Dim fTxtFileName As String
BtrFrm32.List1.AddItem Item
Rem Added to write info to file
fTxtFileName = App.Path & "\" & App.EXEName & ".log"
Open fTxtFileName For Append As #1
Print #1, Item Close #1
Rem *************
End Sub

sub readDate()
PrintLB ("Btrieve Sample Test Started")
PrintLB ("")
Rem Local variables needed for conversion from byte to long.
Dim loc_RecTot As Long
Dim h_field1 As String
Dim h_field2 As String
Dim h_field3 As String
Dim h_field4 As String
Dim h_total As String
'Rem **************************
FileName$ = "XFACE.BTR"
PosBlk$ = Space$(128)KeyBuffer$ = Space$(KEY_BUF_LEN)
RemRem ***************** Btrieve Create *********************Rem
Rem ************* SET UP FILE SPECS
FileBuf.RecLen = 34
FileBuf.PageSize = 1024
FileBuf.IndxCnt = 2
FileBuf.FileFlags = 0
''Rem ************* SET UP KEY SPECS
FileBuf.KeyBuf(0).KeyPos = 1
FileBuf.KeyBuf(0).KeyLen = 8
FileBuf.KeyBuf(0).KeyFlags = EXTTYPE + MODIFIABLE
FileBuf.KeyBuf(0).KeyType = Chr$(BFLOAT)
FileBuf.KeyBuf(1).KeyPos = 9
FileBuf.KeyBuf(1).KeyLen = 26
FileBuf.KeyBuf(1).KeyFlags = EXTTYPE + MODIFIABLE + DUP
FileBuf.KeyBuf(1).KeyType = Chr$(BSTRING)

'Open File
KeyBufLen = KEY_BUF_LEN
KeyBuffer$ = txtFileName.txt
BufLen = Len(DataBuf)KeyNum = 0
Status = BTRCALL(BOPEN, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, KeyNum)
If Status <> 0 Then
Msg$ = "Error Opening file! " + Str$(Status)
PrintLB (Msg$)
GoTo Fini
Else
Msg$ = "File Opened Successfully!" PrintLB (Msg$)
End If

BufLen = Len(DataBuf)
KeyBuffer$ = Space$(255)
KeyBufLen = KEY_BUF_LEN
'Status = BTRCALL(BGETFIRST, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, 0)
If Status <> 0 Then
Msg$ = "Error on BGETFIRST. " + Str$(Status)
PrintLB (Msg$)
Else
Msg$ = "BGETFIRST okay for : " + DataBuf.profDesc PrintLB (Msg$)
End If
'Get Next Record
BufLen = Len(DataBuf)KeyBuffer$ = Space$(KEY_BUF_LEN)
KeyBufLen = KEY_BUF_LEN
Status = BTRCALL(BGETNEXT, PosBlk$, DataBuf, BufLen, ByVal KeyBuffer$, KeyBufLen, 0)
If Status <> 0 Then
Msg$ = "Error on BGETNEXT. " + Str$(Status)
PrintLB (Msg$)
Else
Msg$ = "BGETFIRST okay for : " + DataBuf.profDesc
PrintLB (Msg$)
End If


end sub

Tuesday, October 10, 2006

connecting a database with c# / asp.net

this is the code of database connection and sql executing functions

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

///


/// data base class
///

public class db
{
OleDbConnection gDbConn;
OleDbCommand gDbCmd;
public string error;
public db(string connStr)
{
try
{
gDbCmd = new OleDbCommand();
gDbConn = new OleDbConnection();
gDbConn.ConnectionString = connStr;
error="class built successfully";
}
catch (Exception e)
{
error = "[class constructor] building class faild, error in connecting database error discribe: "+e.Message ;
}
}
public OleDbDataReader executeSQL(String sql)
{
try
{
OleDbDataReader rs ;
gDbConn.Open();
gDbCmd.Connection = gDbConn;
gDbCmd.CommandText = sql;
if (sql.Contains("select") )
rs = gDbCmd.ExecuteReader();
else
{
gDbCmd.ExecuteNonQuery();
rs = null;
}
error = "executeSQL executed successfully";
return rs;
}
catch(Exception e)
{
error = "[execute sql] failed to execute sql statement error discribe:" + e.Message
return null;
}
}
}

an example of using this class here: http://byramix.blogspot.com/2006/10/using-data-base-class.html



Monday, August 21, 2006

Cannot open database requested in login 'databaseName'. Login fails

it happened to me with sql server 2000 and 2005, after restoring the database i couldn't login from asp or asp.net script.
this solution is for sql 2000 but i think it will work also with 2005.
i noticed that after the restore the user that i'm tring to loggin with appears on the logins of the specifice database but the culomn of Login Name is empty. i looked at the same place in the original database and i saw something there... it was lost with the restore! i don't know if it's a bug or what, but i lost few days of fighting with it!
anyway, you need to put the the user name... it's not so easy, i didn't find the way to do it, i had to delete it and add it again.. that also not so simple! so you can delete the login it shouldn;t own nothing... so i had to change all the objects that were owned by this login and then could do it!!
i hope you found this blog before losing too much time on this!! the time that i spent is enough :)
if that helped you so write a commemnt
have a nice programming!!

Discuss Post

Tuesday, May 09, 2006

How to configure SQL Server 2005 to allow remote connections

Enable remote connections for SQL Server 2005 Express or SQL Server 2005 Developer EditionYou must enable remote connections for each instance of SQL Server 2005 that you want to connect to from a remote computer. To do this, follow these steps:
1. Click Start, point to Programs, point to Microsoft SQL Server 2005, point to Configuration Tools, and then click SQL Server Surface Area Configuration.
2. On the SQL Server 2005 Surface Area Configuration page, click Surface Area Configuration for Services and Connections.
3. On the Surface Area Configuration for Services and Connections page, expand Database Engine, click Remote Connections, click Local and remote connections, click the appropriate protocol to enable for your environment, and then click Apply. Note Click OK when you receive the following message:Changes to Connection Settings will not take effect until you restart the Database Engine service.
4. On the Surface Area Configuration for Services and Connections page, expand Database Engine, click Service, click Stop, wait until the MSSQLSERVER service stops, and then click Start to restart the MSSQLSERVER service.

Discuss Post