الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كود الاتصال ب SQL SERVER 2000

مغلق
بدأه hosam_almobyd في 9 يونيو 2004 · 4 رد · 1,791 مشاهدة · في منتدى تصميم صفحات الويب
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

ممكن كود ASP الى بعمل اتصال مع قاعدة بيانات SQL وشكرا لكم

#2

انا شفته هذا الكود عادي

عدي بجيبلك اياه

#3

هنا كتاب لتعلم قواعد بيانات sql server مع asp

ASP Data Access for Beginners SQL.pdf

Technical Lead Developer

My LinkedIn Profile

اللهم قنى شر الجهل و الجهلاء

( اقْتَرَبَ لِلنَّاسِ حِسَابُهُمْ وَهُمْ فِي غَفْلَةٍ مَّعْرِضُونَ ) {الأنبياء:1}

#4

جرب هذا الكود واعلمنا بالنتيجة

----------------------------------

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="Acme2" %>

<html>

<script language="VB" runat="server">

Dim MyConnection As SqlConnection

Sub Page_Load(Sender As Object, E As EventArgs)

myConnection = new SqlConnection("server=192.168.10.5;uid=news123;pwd=123;database=wsyy")

If Not (IsPostBack)
BindGrid()
End If
End Sub

Sub MyDataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)

MyDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub

Sub MyDataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)

MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub

Sub MyDataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)

Dim DS As DataSet
Dim MyCommand As SqlCommand

Dim UpdateCmd As String = "UPDATE dl_zongtable SET fist = @fist, definecode = @definecode, " & _
"pjname = @pjname, num = @num, dwname = @dwname, startime = @startime, " & _
"endtime = @endtime,waittime = @waittime where fist = @fist"




MyCommand = New SqlCommand(UpdateCmd, MyConnection)

MyCommand.Parameters.Add(New SqlParameter("@fist", SqlDbType.Int, 4))
MyCommand.Parameters.Add(New SqlParameter("@definecode", SqlDbType.Int, 4))
MyCommand.Parameters.Add(New SqlParameter("@pjname", SqlDbType.NVarChar, 50))
MyCommand.Parameters.Add(New SqlParameter("@num", SqlDbType.Int, 4))
MyCommand.Parameters.Add(New SqlParameter("@dwname", SqlDbType.NVarChar, 50))
MyCommand.Parameters.Add(New SqlParameter("@startime", SqlDbType.DateTime, 8))
MyCommand.Parameters.Add(New SqlParameter("@endtime", SqlDbType.DateTime, 8))
MyCommand.Parameters.Add(New SqlParameter("@waittime", SqlDbType.NVarChar, 50))


MyCommand.Parameters("@fist").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex))

Dim Cols As String() = {"@fist","@definecode","@pjname","@dwname","@dwname", _
"@startime","@endtime","@waittime"}

Dim NumCols As Integer = E.Item.Cells.Count
Message.InnerHtml = ""

Dim I As Integer
For I=2 To NumCols-1 'skip first, second and last column

Dim CurrentTextBox As System.Web.UI.WebControls.TextBox
CurrentTextBox = E.Item.Cells(I).Controls(0)
Dim ColValue As String = CurrentTextBox.Text


' check for invalid values
Select Case Cols(i-1)
Case "@definecode"
If Not InputValidator.IsValidFiveDigitZipCode(colvalue)
Message.InnerHtml &= "ERROR: Last Name - " & InputValidator.AnsiBasicZipCodeErrorString & "<br>"
End If
Case "@pjname"
If Not InputValidator.IsValidAnsiName(colvalue)
Message.InnerHtml &= "ERROR: First Name - " & InputValidator.AnsiNameErrorString & "<br>"
End If
Case "@num"
If Not InputValidator.IsValidFiveDigitZipCode(colvalue)
Message.InnerHtml &= "ERROR: num - " & InputValidator.AnsiBasicZipCodeErrorString & "<br>"
End If
Case "@dwname"
If Not InputValidator.IsValidAnsiName(colvalue)
Message.InnerHtml &= "ERROR: dwname - " & InputValidator.AnsiNameErrorString & "<br>"
End If
Case "@startime"
If Not InputValidator.IsValidAnsiDateTime(colvalue)
Message.InnerHtml &= "ERROR: startime - " & InputValidator.AnsiDateTimeErrorString & "<br>"
End If
Case "@endtime"
If Not InputValidator.IsValidAnsiDateTime(colvalue)
Message.InnerHtml &= "ERROR: endtime - " & InputValidator.AnsiDateTimeErrorString & "<br>"
End If
Case "@waittime"
If Not InputValidator.IsValidAnsiName(colvalue)
Message.InnerHtml &= "ERROR: waittime Code - " & InputValidator.AnsiNameErrorString & "<br>"
End If
End Select

' Check for null values in required fields
If I<6 And ColValue = ""
Message.InnerHtml &= "ERROR: Null values not allowed for " & Cols(i-1) & "<br>"
End If

MyCommand.Parameters(Cols(I-1)).Value = ColValue
Next

If Message.InnerHtml <> ""
Message.Style("color") = "red"
Return
End If


MyCommand.Connection.Open()

Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>Record Updated</b><br>" & UpdateCmd.ToString()
MyDataGrid.EditItemIndex = -1
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "ERROR: A record already exists with the same " & _
"primary key"
Else
Message.InnerHtml = "ERROR: Could not update record, please " & _
"ensure the fields are correctly filled out"
End If
Message.Style("color") = "red"
End Try

MyCommand.Connection.Close()

BindGrid()
End Sub

Sub BindGrid()

Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = new SqlDataAdapter("select * from dl_zongtable", MyConnection)

DS = new DataSet()
MyCommand.Fill(DS, "dl_zongtable")

MyDataGrid.DataSource=DS.Tables("dl_zongtable").DefaultView
MyDataGrid.DataBind()
End Sub

</script>

<body style="font: 10pt verdana">

<form runat="server">

<h3><font face="Verdana">Updating a Row of Data</font></h3>

<span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>

<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
OnUpdateCommand="MyDataGrid_Update"
DataKeyField="fist"
>

<Columns>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" 
UpdateText="Update" ItemStyle-Wrap="false"/>
</Columns>

</ASP:DataGrid>

</form>

</body>
</html>


SQL SERVER 

Updating a Row of Data
ERROR: Could not update record, please ensure the fields are correctly filled out
fist definecode pjname num dwname startime endtime waittime 
Update Cancel 21 1008 name 100 fjkk 2004-5-12 19:54:49 2004-5-14 19:54:49 2days0hours0minutes 
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
// 
// Copyright (C) Microsoft Corporation. All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acme2
{
public class InputValidator
{
// ***
// Regular Expression useful in Validation
//

// Accepts only digits. No spaces before or after are
// allowed. No monetary symbol is allowed. No decimal
// separator is allowed.
public static string AnsiDateTimeExpressionString = "^[\\d\\s\\:\\-]*$";
public static string AnsiDateTimeErrorString = "Limit characters to ANSI digits,space,and these symbols :-";
public static Regex AnsiDateTimeExpression = new Regex(AnsiDateTimeExpressionString, RegexOptions.Compiled);



public static string AnsiNumericExpressionString = "^\\d*$";
public static string AnsiNumericErrorString = "Limit characters to ANSI digits";
public static Regex AnsiNumericExpression = new Regex(AnsiNumericExpressionString, RegexOptions.Compiled);

// Accepts only basic ANSI letters, digits, the period
// character, and space characters. Requires that at
// least one character exist to be validated.
public static string AnsiNameExpressionString = "^[\\w\\d\\.\\s\\,\\']*$";
public static string AnsiNameErrorString = "Limit characters to alphanumeric, spaces, and these symbols .,'";
public static Regex AnsiNameExpression = new Regex(AnsiNameExpressionString, RegexOptions.Compiled);

// Looks for characters that can be considered unsafe text.
// Items to watch out for include quotes, apostrophes,
// semi-colons, and the less than and greater than signs.
// Unknown malicious characters can easily bypass the
// IsSafeText expression.
public static string SafeTextExpressionString = "[\\\"\\'\\;<>]";
public static string SafeTextErrorString = "String cannot contain these symbols <>;'\"";
public static Regex SafeTextExpression = new Regex(SafeTextExpressionString, RegexOptions.Compiled);

// Accepts only valid email characters.
// Valid email characters are limited to
// Alphanumeric, periods, underscores, and the
// at symbol.
public static string AnsiEmailExpressionString = "^[\\w\\d\\.\\@_]*$";
public static string AnsiEmailErrorString = "Limit characters to alphanumeric and these symbols _.@";
public static Regex AnsiEmailExpression = new Regex(AnsiEmailExpressionString, RegexOptions.Compiled);

// Accepts only valid telephone number characters.
// This restricts the input to digits, hyphens, parentheses,
// periods, commas, space characters, the plus symbol,
// and ansi letters.
// This expression can be customized to add better parsing
// and to limit input types.
//
// Examples of valid input
// 111 111 1111
// (111) 111-1111
// (111) 111-1111 ext. 555
public static string AnsiPhoneExpressionString = "^[\\w\\d\\.\\s\\(\\)\\+\\,\\-]*$";
public static string AnsiPhoneErrorString = "Limit characters to ANSI alphanumerics, spaces, and these symbols .,+-()";
public static Regex AnsiPhoneExpression = new Regex(AnsiPhoneExpressionString, RegexOptions.Compiled);

// Accepts only valid address input characters.
// This restricts the input to ansi letters, digits,
// commas, hyphens, periods, semi-colons, and space
// characters.
public static string AnsiAddressExpressionString = "^[\\w\\d\\.\\s\\;\\,\\-]*$";
public static string AnsiAddressErrorString = "Limit characters to ANSI alphanumerics, spaces, and these symbols .,-;";
public static Regex AnsiAddressExpression = new Regex(AnsiAddressExpressionString, RegexOptions.Compiled);

// Accepts only two character states
// This can be extended to actually check against a valid
// listing of states. This is a first tier approach that
// can be much easier to implement.
public static string AnsiTwoCharacterStateExpressionString = "^[\\w]{2}$";
public static string AnsiTwoCharacterStateErrorString = "Limit to 2 ANSI alpha characters";
public static Regex AnsiTwoCharacterStateExpression = new Regex(AnsiTwoCharacterStateExpressionString, RegexOptions.Compiled);

// Accepts only alpha state or city names with periods.
// This can be extended to check against actual
// state names. This is a first tier approach that
// can be much easier to implement.
public static string AnsiCityStateExpressionString = "^[\\w\\s\\.\\'\\,]*$";
public static string AnsiCityStateErrorString = "Limit characters to ANSI alpha characters, spaces, and these symbols .,'";
public static Regex AnsiCityStateExpression = new Regex(AnsiCityStateExpressionString, RegexOptions.Compiled);

// Accepts only 5 character zip codes
public static string AnsiBasicZipCodeExpressionString = "^[\\d]*$";
public static string AnsiBasicZipCodeErrorString = "Limit to digits";
public static Regex AnsiBasicZipCodeExpression = new Regex(AnsiBasicZipCodeExpressionString, RegexOptions.Compiled);

// Accepts only 9 character zip codes
// with a hyphen separator between digits
// 5 and 6
public static string AnsiExtendedZipCodeExpressionString = "^\\d{5}\\-\\d{4}$";
public static string AnsiExtendedZipCodeErrorString = "Limit to 5 ANSI digits, a hyphen, and 4 more ANSI digits";
public static Regex AnsiExtendedZipCodeExpression = new Regex(AnsiExtendedZipCodeExpressionString, RegexOptions.Compiled);

// ***
// Regular Expression useful in string stripping and conversion
//
public static string StripHtmlTagsExpressionString = "\\<\\.\\*\\>";
public static Regex StripHtmlTagsExpression = new Regex(StripHtmlTagsExpressionString, RegexOptions.Compiled);

public static string StripHtmlTagCharactersExpressionString = "[\\<\\>]";
public static Regex StripHtmlTagCharactersExpression = new Regex(StripHtmlTagCharactersExpressionString, RegexOptions.Compiled);

public static string EscapeApostrophesExpressionString = "[\\']";
public static Regex EscapeApostrophesExpression = new Regex(EscapeApostrophesExpressionString, RegexOptions.Compiled);

public static string EscapeNewLinesToHtmlExpressionString = "(?:\\r\\n|\\n\\r|\\r|\\n)";
public static Regex EscapeNewLinesToHtmlExpression = new Regex(EscapeNewLinesToHtmlExpressionString, RegexOptions.Compiled);

// ***
// String Normalization Methods
//

public static string StripHtmlTags(string text)
{
return StripHtmlTagsExpression.Replace(text, "");
}

// This method is multi input hack safe. This means
// that tags spread over multiple inputs won't miss
// being stripped. For instance:
//
// input 1: <script
// input 2: language=javascript>alert('me');
//
// This would get parsed out because the tag
// begin and end symbols get ripped. Another
// stripping approach could be to strip the tag
// characters plus whatever is inside, but this
// doesn't work when input is spread over multiple
// controls.
public static string StripHtmlTagsSafe(string text)
{
return StripHtmlTagCharactersExpression.Replace(text, "");
}

public static string EscapeApostrophes(string text)
{
return EscapeApostrophesExpression.Replace(text, "''");
}

// If you are going to use this method then call
// StripHtmlTags first, then call this method. Else
// you'll strip out the BR's.
public static string EscapeNewLinesToHtml(string text)
{
return EscapeNewLinesToHtmlExpression.Replace(text, "<BR>");
}

// ***
// Numeric Validator Methods
//

// Parses valid ansi numbers
public static bool IsValidAnsiDateTime(string number)
{
return AnsiDateTimeExpression.IsMatch(number);
}
public static bool IsValidAnsiNumber(string number)
{
return AnsiNumericExpression.IsMatch(number);
}

// Parses 5 digit zip codes
public static bool IsValidFiveDigitZipCode(string zip)
{
return AnsiBasicZipCodeExpression.IsMatch(zip);
}

// Parses 9 digit zip codes
public static bool IsValidNineDigitZipCode(string zip)
{
return AnsiExtendedZipCodeExpression.IsMatch(zip);
}

// Attempts to parse the number based on the current
// locale. This can be used for international support.
public static bool IsParseableNumber(string number)
{
try
{
Int32.Parse(number);
return true;
}
catch
{
}

return false;
}

// ***
// String Data Validators
//

// Processes a name value
public static bool IsValidAnsiName(string name)
{
return AnsiNameExpression.IsMatch(name);
}

// Processes phone number values
public static bool IsValidAnsiPhoneNumber(string name)
{
return AnsiPhoneExpression.IsMatch(name);
}

// Processes address values
public static bool IsValidAnsiAddress(string address)
{
return AnsiAddressExpression.IsMatch(address);
}

// Processes two character state strings
public static bool IsValidAnsiTwoCharacterState(string state)
{
return AnsiTwoCharacterStateExpression.IsMatch(state);
}

// Processes city or state strings
public static bool IsValidAnsiCityOrState(string citystate)
{
return AnsiCityStateExpression.IsMatch(citystate);
}

// Processes safe text
public static bool IsSafeText(string text)
{
return SafeTextExpression.Matches(text).Count == 0;
}
}
}
#5

السلام عليكم

ببساطة، استخدم الكود التالي مع التعديلات الملائمة

DBuserID = "SQL Server Username Here"
DBuserPassword = "SQL Server Password Here"
dbFileName = "Database Name or Library Name Here"
DBserverID = "SQL Server Name Here"

Conection_String = "DRIVER={SQL Server};UID=" & DBuserID & ";password=" & DBuserPassword & ";DATABASE=" & dbFileName & ";SERVER=" & DBserverID & ""

Mohamad Al-Karbi

www.mkarbi.me

هذا الموضوع مغلق.

مواضيع مشابهة