PDA

View Full Version : سوال: جستجوی رکورد دیتابیس Access در Visual Studio .Net 2010



djhooman
یک شنبه 30 آبان 1389, 21:37 عصر
سلام دوستان ،


من تقریبا 2 هفته هست که دارم توی سایت میچرخم تا ببینم کسی مشکل من رو داشته تا پست اضافه نزنم که دیدم همچین مساله ای نبوده !!!!


من یه پروژه دارم که فیلد اصلی و مهم دیتابیسم شماره پرونده مراجعه کننده هست که همین فیلد Primary Key هستش !
یه فرم ثبت اطلاعات هم دارم ....
برای ثبت ، حذف و ویرایش مشکلی ندارم ، اما جستجو ....



یه فرم دیگه ساختم ، توی اون فرم یه Textbox ساختم به نام textbox1 که میخوام موقعی که کاربر عددی رو توی اون وارد میکنه ، textbox1 مقدار عدد وارد شده رو ببره توی دیتابیس و شروع کنه بگرده ، اگه پیدا کردتش تمام اطلاعات اون سطر رو در Data Grid نمایش بده !!!!!!!

لازم به ذکره که بانک اطلاعاتی Access هستش .


ممنون میشم راهنماییم کنید .
چند تا کد گیر آوردم اما اونا همش اس کیو ال بود که به دردم نمیخورد ....

yekta64
دوشنبه 01 آذر 1389, 09:49 صبح
سلام دوستان ،


من تقریبا 2 هفته هست که دارم توی سایت میچرخم تا ببینم کسی مشکل من رو داشته تا پست اضافه نزنم که دیدم همچین مساله ای نبوده !!!!


من یه پروژه دارم که فیلد اصلی و مهم دیتابیسم شماره پرونده مراجعه کننده هست که همین فیلد Primary Key هستش !
یه فرم ثبت اطلاعات هم دارم ....
برای ثبت ، حذف و ویرایش مشکلی ندارم ، اما جستجو ....



یه فرم دیگه ساختم ، توی اون فرم یه Textbox ساختم به نام textbox1 که میخوام موقعی که کاربر عددی رو توی اون وارد میکنه ، textbox1 مقدار عدد وارد شده رو ببره توی دیتابیس و شروع کنه بگرده ، اگه پیدا کردتش تمام اطلاعات اون سطر رو در Data Grid نمایش بده !!!!!!!

لازم به ذکره که بانک اطلاعاتی Access هستش .


ممنون میشم راهنماییم کنید .
چند تا کد گیر آوردم اما اونا همش اس کیو ال بود که به دردم نمیخورد ....


دوست عزیز
شما باید در رویداد keypress تکسباکستون چک کنید اگه کلید اینتر فشرده شد محتوای تکس باکس به یه تابع ارسال بشه که کار این تابع این هستش که با استفاده از یه کوری select رکود مذکور رو از دیتابیس برگردونه و در datagrid نمایش بده این تابع به شکل زیز هستش::



Public Function hhh() As Integer
Dim StrSql As String
Dim Da As New OleDbDataAdapter
Dim ds As New DataSet
Dim StrCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\DataBaseName.accdb"
Dim Con As OleDbConnection = New OleDbConnection(StrCon)
Con.Open()
StrSql = "SELECT * FROM tblName WHERE PNumber = '" & textbox1.Text & "' "
Dim Command As New OleDbCommand(StrSql, Con)
Da = New OleDbDataAdapter(Command)
Da.Fill(ds, "tbl")
DataGridView1.DataSourec = ds
End Function


این تابع رکوردی رو براساس شماره پرونده ی که کتربر در تکس باکس نوشته ب می گردونه

موفق باشید.

djhooman
دوشنبه 01 آذر 1389, 10:55 صبح
میشه بگید این و چجوری بکار ببرم ؟

اگه میشه متغییر هاش و برام تغییر بدید

اسم دستابیس : DataBase.Mdb

اسم تیبلی که میخوام سرچ کنه ستون ها رو توش : EtelatMoshtarianBindingSource

اسم تکست باکسم : TextBox1


خیلی باهاش ور رفتم اما نشد !!!

درضمن ورژن Visual Studio من هم 2010 هستش !!!!

yekta64
دوشنبه 01 آذر 1389, 12:01 عصر
میشه بگید این و چجوری بکار ببرم ؟

اگه میشه متغییر هاش و برام تغییر بدید

اسم دستابیس : DataBase.Mdb

اسم تیبلی که میخوام سرچ کنه ستون ها رو توش : EtelatMoshtarianBindingSource

اسم تکست باکسم : TextBox1


خیلی باهاش ور رفتم اما نشد !!!

درضمن ورژن Visual Studio من هم 2010 هستش !!!!

سلام
اکسس شما 2003 هستش؟؟؟؟

ببین دوست من شما می خوای وقتی کاربر عدد مربوط به شماره پرونده رو در تکس باکس وارد کرده و کلید اینتر رو زد بره و رکورد ی با این شماره پرونده رو بیاره و در گرید ویو نمایش بده

خوب شما اول در رویداد keyDown تکس باکس چک می کنی اگه اینتر زده شد تابع مربوط به بازگردون رکورد از دیتابیس فراخوانی بشه



Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
hhh()
End If
End Sub



تابع hhh هم با توجه به اطلاعات شما تغییر دادم فقط اسم فیلد شماره پرونده رو pNumber گذاشتم که شما خودتون تعییر بدید:



Public Function hhh() As Integer
Dim StrSql As String
Dim Da As New OleDbDataAdapter
Dim ds As New DataSet
Dim StrCon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\DataBase.mdb"
Dim Con As OleDbConnection = New OleDbConnection(StrCon)
Con.Open()
StrSql = "SELECT * FROM tblName WHERE PNumber = '" & TextBox1.Text & "' "
Dim Command As New OleDbCommand(StrSql, Con)
Da = New OleDbDataAdapter(Command)
Da.Fill(ds, "tbl")
DataGridView1.DataSourec = ds
End Function



فقط به این نکته توجه کنید که دیتابیس رو باید در پوشه debug برنامه قرار بدید

بازم اگر مشکلی بود مطرح کنید.

djhooman
دوشنبه 01 آذر 1389, 12:43 عصر
این کارارو کردم ،
اما این اررور و داد .....



System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217865
Message=The Microsoft Access database engine cannot find the input table or query 'Etelate'. Make sure it exists and that its name is spelled correctly.
Source=Microsoft Access Database Engine
StackTrace:
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextE rrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextF orSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText( Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(Comm andBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(Comma ndBehavior behavior)
at System.Data.OleDb.OleDbCommand.System.Data.IDbComm and.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(Data Set dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at C:\Users\Hooman\Desktop\Search-Parvande.vb:line 77
at .Search_Parvande.TextBox1_KeyDown(Object sender, KeyEventArgs e) in C:\Users\Hooman\Desktop\1103\Search-Parvande.vb:line 61
at System.Windows.Forms.Control.OnKeyDown(KeyEventArg s e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(M essage& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Mes sage& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at.Main.SearchMenuParvande_Click(Object sender, EventArgs e) in C:\Users\Hooman\Desktop 1103\Main.vb:line 103
at System.Windows.Forms.ToolStripItem.RaiseEvent(Obje ct key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(M ouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventIntera ctive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(Event Args e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEven tArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(M ouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Mes sage& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at د\Main.Designer.vb:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

yekta64
دوشنبه 01 آذر 1389, 14:09 عصر
این کارارو کردم ،
اما این اررور و داد .....



System.Data.OleDb.OleDbException was unhandled
ErrorCode=-2147217865
Message=The Microsoft Access database engine cannot find the input table or query 'Etelate'. Make sure it exists and that its name is spelled correctly.
Source=Microsoft Access Database Engine
StackTrace:
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextE rrorHandling(OleDbHResult hr)
at System.Data.OleDb.OleDbCommand.ExecuteCommandTextF orSingleResult(tagDBPARAMS dbParams, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommandText( Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(Comm andBehavior behavior, Object& executeResult)
at System.Data.OleDb.OleDbCommand.ExecuteReaderIntern al(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(Comma ndBehavior behavior)
at System.Data.OleDb.OleDbCommand.System.Data.IDbComm and.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(Data Set dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
at C:\Users\Hooman\Desktop\Search-Parvande.vb:line 77
at .Search_Parvande.TextBox1_KeyDown(Object sender, KeyEventArgs e) in C:\Users\Hooman\Desktop\1103\Search-Parvande.vb:line 61
at System.Windows.Forms.Control.OnKeyDown(KeyEventArg s e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(M essage& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Mes sage& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at.Main.SearchMenuParvande_Click(Object sender, EventArgs e) in C:\Users\Hooman\Desktop 1103\Main.vb:line 103
at System.Windows.Forms.ToolStripItem.RaiseEvent(Obje ct key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(Eve ntArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(M ouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventIntera ctive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(Event Args e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEven tArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(M ouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Mes sage& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Mes sage& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at د\Main.Designer.vb:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:


اگه بتونید پروژه تون رو بذارید ببینم مشکل چی هستش

موفق باشید