File size: 1,358 Bytes
484fe03
 
 
6438c5f
 
 
9ae496e
484fe03
6438c5f
484fe03
6438c5f
484fe03
6438c5f
 
 
484fe03
6438c5f
 
484fe03
6438c5f
9ae496e
6438c5f
 
 
 
 
 
 
9ae496e
484fe03
9ae496e
6438c5f
 
 
 
 
 
 
9ae496e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import streamlit as st
import pyodbc

# Fixed SQL Server details
SERVER = "indsr-foundry.database.windows.net"
DATABASE = "foundry_db_monarch"
DRIVER = "ODBC Driver 17 for SQL Server"  # Use the correct driver name

st.set_page_config(page_title="SQL Server Connection Test", layout="centered")

st.title("SQL Server Connection Test")

# User inputs for authentication
username = st.text_input("Database Username", placeholder="Enter your username")
password = st.text_input("Database Password", type="password", placeholder="Enter your password")

def test_connection(username, password):
    """Attempts to connect to the SQL Server and returns success or failure message."""
    try:
        conn_str = (
            f"DRIVER={{{DRIVER}}};"
            f"SERVER={SERVER};"
            f"DATABASE={DATABASE};"
            f"UID={username};"
            f"PWD={password};"
        )
        conn = pyodbc.connect(conn_str, timeout=5)
        conn.close()
        return "✅ Connection successful! You are connected to SQL Server."
    except Exception as e:
        return f"❌ Connection failed: {str(e)}"

# Button to test connection
if st.button("Test Connection"):
    if username and password:
        result = test_connection(username, password)
        st.write(result)
    else:
        st.write("⚠️ Please enter both username and password.")