Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from sqlalchemy import create_engine, text | |
| # Fixed SQL Server details | |
| SERVER = "indsr-foundry.database.windows.net" | |
| DATABASE = "foundry_db_monarch" | |
| DRIVER = "ODBC Driver 17 for SQL Server" # Make sure this matches the installed driver | |
| 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 using SQLAlchemy + pyodbc.""" | |
| try: | |
| conn_str = ( | |
| f"mssql+pyodbc://{username}:{password}@{SERVER}/{DATABASE}?" | |
| f"driver={DRIVER}&Encrypt=yes&TrustServerCertificate=no" | |
| ) | |
| engine = create_engine(conn_str, pool_pre_ping=True) | |
| with engine.connect() as connection: | |
| result = connection.execute(text("SELECT 1")) | |
| 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.") | |