Alsalam alikom
i have a project to develope a windows forms that connect to the database throught a web service
all my application must connect to database throght web service
any sugesstions
Alsalam alikom
i have a project to develope a windows forms that connect to the database throught a web service
all my application must connect to database throght web service
any sugesstions
ماذا تريده أن يفعل برنامجك في قاعدة البيانات بعد اتصاله بها: هل تريد من دالة الweb service أن ترجع dataset أو datareader كامل؟
أم تريدها أن تجري المعالجة ثم ترجع ناتج التنفيذ أو ترجع error code يدل على نجاح المعالجة من فشلها؟
وضح المطلوب في مشروعك أكثر لنستطيع مساعدتك. :)
تم تعديل هذه المشاركة بواسطة walcom في 11 فبراير 2005 في 00:11
فكر بطريقة أخرى
yes i need to connect to the database throght the web service to update records and delete and insert new records
no one can access this data base except thoght this web service
i really don't know why they need that but this how they asked me to do
thank you
yes i need to connect to the database throght the web service to update records and delete and insert new records
ستبدأ بمشروع جديد من نوع ASP.Net Web service ، وانتظر قليلا ريثما ينتهي إنشاء المشروع. ستجد صفحة من اسمها service1.asmx.vb وهي الصفحة الرئيسية للweb service وسيكون عملنا عليها.
انتقل للكود، وأضف في الأعلى:
imports System.Data.SqlClient
ثم أنشيء web method كما يلي:
<WebMethod()> Public Function SaveNewCustomer( _
ByVal c_name As String, ByVal c_address As String, _
ByVal c_tel As String, ByVal c_email As String _
) As Integer
' لبدء الاتصال
try
Dim strcn As String = "server=(local); database=Issuer; uid=sa; trusted_connection=yes"
Dim cn As SqlConnection = New SqlConnection(strcn)
' the save way you do in a normal windows form application
sql = "Insert into tblCustomers values('" + c_name + "', '"+ c_address + "', '" + c_tel + "', '" + c_email +"')"
cn.Open()
Dim cmd As SqlCommand = New SqlCommand(sql, cn)
dim succeeded as boolean
succeeded = cmd.ExecuteNonQuery()
if succeeded then
return 1 ' to show that the save operation succeeded
else
return 0 ' to show that the save operation failed
end if
Catch ex As Exception
msgbox(ex.Message)
end tryوباقي العمليات بنفس الكيفية، قم بعمل build للمشروع وأغلقه، وابدأ مشروعا جديدا من نوع windows application أو النوع الذي تريده.
لاستعمال الweb service :
من قائمة project اختر add web reference، ستظهر لك كما في الصورة أدناه:

اضغط على web services on the local machine وسيقوم بالبحث عن كل الweb services الموجودة في جهازك، اختر واحدا منها أو أكتب مسار ملف الasmx في URL، مثلا:
http://localhost/myWebService/Service1.asmx
انتظر قليلا ريثما يقوم بالبحث عن الweb service والتعرف عليها، عند الانتهاء اضغط على
Add Reference، الآن ستلاحظ ظهورها في الsolution explorer ضمن web reference، والآن لاستعمال الweb service فعليا:
try
' to create the object that will be used to reference and consume the web service
Dim srvce As New localhost.Service1
Dim ErrCode As Integer
' now we are going to access the web method defined in the web service
' notice that it is the same as calling a method in a classs
ErrCode = srvce.SaveNewCustomer(me.txtName.text, me.txtAddress.text, me.txtTelephone.text, me.txtEmail.text)
Catch ex As Exception
msgbox(ex.Message)
end try
select case ErrCode
case 0
msgbox("لم يتم الحفظ", MsgBoxStyle.Exclamation)
case 1
msgbox("تم الحفظ بنجاح", MsgBoxStyle.Information)
end selectبالتوفيق بإذن الله.
فكر بطريقة أخرى
Gazakom Allah khaira
w nafa3 bikom inshaa Allah
i will try it and inshaa Allah inform you
but first i will translate it into CSharp :D
thanx again
تم تعديل هذه المشاركة بواسطة aroma في 12 فبراير 2005 في 21:58
السلام عليكم اخ walcom
الصراحة شرحك حلو كتير
بس ممكن اعرف شو ممكن اسوي لو كنت بدي اكتب الكود هادا بالسي شارب c#
وشكرا لك كثيرا
ابو علاء
السلام عليكم
using System.Data.SqlClient;
[WebMethod()]
public int SaveNewCustomer(string c_name, string c_address, string c_tel, string c_email) {
try {
string strcn = "server=(local); database=Issuer; uid=sa; trusted_connection=yes";
SqlConnection cn = new SqlConnection(strcn);
sql = ("Insert into tblCustomers values(\'"
+ (c_name + ("\', \'"
+ (c_address + ("\', \'"
+ (c_tel + ("\', \'"
+ (c_email + "\')"))))))));
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
bool succeeded;
succeeded = cmd.ExecuteNonQuery();
if (succeeded) {
return 1;
}
else {
return 0;
}
}
catch (Exception ex) {
msgbox(ex.Message);
}
}{وَاتَّقُواْ يَوْمًا تُرْجَعُونَ فِيهِ إِلَى اللّهِ ثُمَّ تُوَفَّى كُلُّ نَفْسٍ مَّا كَسَبَتْ وَهُمْ لاَ يُظْلَمُونَ} (281) سورة البقرة

السلام عليكم.
من خلال اللصورة المرفقة ورسالة الخطاء ،ربما كانت المشكلة انك استعملت ال messageboxمن داخل ال web service
اخي عملت خطوة خطوة المثال الموجود في الاعلى دون اضافة اي شي جديد
هذا الموضوع مغلق.