Code Monkey home page Code Monkey logo

tdslib's Introduction

TdsLib

Nuget

This repository contains an open implementation of the TDS protocol (version 7.4) in managed C# code. It is not a full implementation, current support is focused on the login steps and some generic TDS features. It does not support normal data streams or T-SQL commands.

This library is mainly used for probing and for diagnosing TDS connections but can be used to manually construct and handle any step of a TDS connection. It can also be used to mock TDS clients.

Requirements

Code structure

src
  Microsoft.Data.Tools.TdsLib - TdsLib code
    Buffer - Buffer class to handle type conversion
    IO - Connection related classes
    Messages - Logical message format used to generate Packets to be sent to SQL Server
    Packets - Packet related classes
    Payloads - Message Payloads
    Tokens - Tokens that can be received from SQL server

test
  Microsoft.Data.Tools.TdsLib.UnitTest - Unit tests
  Microsoft.Data.Tools.TdsLib.IntegrationTest - Integration tests

Example

Establish a TDS connection and login to a SQL server

using Microsoft.Data.Tools.TdsLib;
using Microsoft.Data.Tools.TdsLib.IO.Connection.Tcp;
using Microsoft.Data.Tools.TdsLib.Messages;
using Microsoft.Data.Tools.TdsLib.Packets;
using Microsoft.Data.Tools.TdsLib.Payloads.Login7;
using Microsoft.Data.Tools.TdsLib.Payloads.PreLogin;
using Microsoft.Data.Tools.TdsLib.Tokens.Error;

string hostname = "sqlserver.contoso.net";
int port = 1433;

using TdsClient client = new TdsClient(new TcpServerEndpoint(hostname, port));

// Send PreLogin message
await client.MessageHandler.SendMessage(new Message(PacketType.PreLogin)
{
    Payload = new PreLoginPayload(encrypt: true)
});

// Receive PreLogin message
var preLoginResponseMessage = await client.MessageHandler.ReceiveMessage(b => new PreLoginPayload(b));

// Perform TLS handshake
await client.PerformTlsHandshake();

// Prepare Login7 message
Login7Payload login7Payload = new Login7Payload()
{
    Hostname = hostname,
    ServerName = "MyServerName",
    AppName = "MyAppName",
    Language = "us_english",
    Database = "MyDatabaseName",
    //Username,
    //Password
};

login7Payload.TypeFlags.AccessIntent = OptionAccessIntent.ReadWrite;

Message login7Message = new Message(PacketType.Login7) { Payload = login7Payload };

// Send Login7 message
await client.MessageHandler.SendMessage(login7Message);

// Receive Login response tokens
await client.TokenStreamHandler.ReceiveTokensAsync(tokenEvent =>
{
    if (tokenEvent.Token is ErrorToken errorToken)
    {
        // Error was received from the server
    }
});

// If no error token was received, and SQL server did not close the connection, then the connection to the server is now established and the user is logged in.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Trademarks

This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.

tdslib's People

Contributors

adityastic avatar conort38 avatar kamilprz avatar lupino3 avatar microsoft-github-operations[bot] avatar microsoftopensource avatar valterc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

mahaisong rwood

tdslib's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.